Skip to content

Commit b1f0c0a

Browse files
committed
rustc_resolve: flatten nested ifs
1 parent 2718a9a commit b1f0c0a

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
227227
let (name, span) =
228228
(ident.name, self.tcx.sess.source_map().guess_head_span(new_binding.span));
229229

230-
if let Some(s) = self.name_already_seen.get(&name) {
231-
if s == &span {
232-
return;
233-
}
230+
if let Some(s) = self.name_already_seen.get(&name)
231+
&& s == &span
232+
{
233+
return;
234234
}
235235

236236
let old_kind = match (ns, old_binding.module()) {
@@ -382,20 +382,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
382382
suggestion = Some(format!("self as {suggested_name}"))
383383
}
384384
ImportKind::Single { source, .. } => {
385-
if let Some(pos) =
386-
source.span.hi().0.checked_sub(binding_span.lo().0).map(|pos| pos as usize)
385+
if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
386+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span)
387+
&& pos as usize <= snippet.len()
387388
{
388-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span) {
389-
if pos <= snippet.len() {
390-
span = binding_span
391-
.with_lo(binding_span.lo() + BytePos(pos as u32))
392-
.with_hi(
393-
binding_span.hi()
394-
- BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
395-
);
396-
suggestion = Some(format!(" as {suggested_name}"));
397-
}
398-
}
389+
span = binding_span.with_lo(binding_span.lo() + BytePos(pos)).with_hi(
390+
binding_span.hi() - BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
391+
);
392+
suggestion = Some(format!(" as {suggested_name}"));
399393
}
400394
}
401395
ImportKind::ExternCrate { source, target, .. } => {
@@ -512,13 +506,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
512506
// If the first element of our path was actually resolved to an
513507
// `ExternCrate` (also used for `crate::...`) then no need to issue a
514508
// warning, this looks all good!
515-
if let Some(binding) = second_binding {
516-
if let NameBindingKind::Import { import, .. } = binding.kind {
517-
// Careful: we still want to rewrite paths from renamed extern crates.
518-
if let ImportKind::ExternCrate { source: None, .. } = import.kind {
519-
return;
520-
}
521-
}
509+
if let Some(binding) = second_binding
510+
&& let NameBindingKind::Import { import, .. } = binding.kind
511+
// Careful: we still want to rewrite paths from renamed extern crates.
512+
&& let ImportKind::ExternCrate { source: None, .. } = import.kind
513+
{
514+
return;
522515
}
523516

524517
let diag = BuiltinLintDiag::AbsPathWithModule(root_span);
@@ -1217,12 +1210,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12171210
}
12181211

12191212
// #90113: Do not count an inaccessible reexported item as a candidate.
1220-
if let NameBindingKind::Import { binding, .. } = name_binding.kind {
1221-
if this.is_accessible_from(binding.vis, parent_scope.module)
1222-
&& !this.is_accessible_from(name_binding.vis, parent_scope.module)
1223-
{
1224-
return;
1225-
}
1213+
if let NameBindingKind::Import { binding, .. } = name_binding.kind
1214+
&& this.is_accessible_from(binding.vis, parent_scope.module)
1215+
&& !this.is_accessible_from(name_binding.vis, parent_scope.module)
1216+
{
1217+
return;
12261218
}
12271219

12281220
let res = name_binding.res();
@@ -1255,14 +1247,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12551247
segms.push(ast::PathSegment::from_ident(ident));
12561248
let path = Path { span: name_binding.span, segments: segms, tokens: None };
12571249

1258-
if child_accessible {
1250+
if child_accessible
12591251
// Remove invisible match if exists
1260-
if let Some(idx) = candidates
1252+
&& let Some(idx) = candidates
12611253
.iter()
12621254
.position(|v: &ImportSuggestion| v.did == did && !v.accessible)
1263-
{
1264-
candidates.remove(idx);
1265-
}
1255+
{
1256+
candidates.remove(idx);
12661257
}
12671258

12681259
if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
@@ -1319,12 +1310,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13191310
let is_extern_crate_that_also_appears_in_prelude =
13201311
name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
13211312

1322-
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
1313+
if (!is_extern_crate_that_also_appears_in_prelude || alias_import)
13231314
// add the module to the lookup
1324-
if seen_modules.insert(module.def_id()) {
1325-
if via_import { &mut worklist_via_import } else { &mut worklist }
1326-
.push((module, path_segments, child_accessible, child_doc_visible));
1327-
}
1315+
&& seen_modules.insert(module.def_id())
1316+
{
1317+
if via_import { &mut worklist_via_import } else { &mut worklist }.push((
1318+
module,
1319+
path_segments,
1320+
child_accessible,
1321+
child_doc_visible,
1322+
));
13281323
}
13291324
}
13301325
})
@@ -1547,19 +1542,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15471542
macro_kind.descr_expected(),
15481543
),
15491544
};
1550-
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
1551-
if !import.span.is_dummy() {
1552-
let note = errors::IdentImporterHereButItIsDesc {
1553-
span: import.span,
1554-
imported_ident: ident,
1555-
imported_ident_desc: &desc,
1556-
};
1557-
err.subdiagnostic(note);
1558-
// Silence the 'unused import' warning we might get,
1559-
// since this diagnostic already covers that import.
1560-
self.record_use(ident, binding, Used::Other);
1561-
return;
1562-
}
1545+
if let crate::NameBindingKind::Import { import, .. } = binding.kind
1546+
&& !import.span.is_dummy()
1547+
{
1548+
let note = errors::IdentImporterHereButItIsDesc {
1549+
span: import.span,
1550+
imported_ident: ident,
1551+
imported_ident_desc: &desc,
1552+
};
1553+
err.subdiagnostic(note);
1554+
// Silence the 'unused import' warning we might get,
1555+
// since this diagnostic already covers that import.
1556+
self.record_use(ident, binding, Used::Other);
1557+
return;
15631558
}
15641559
let note = errors::IdentInScopeButItIsDesc {
15651560
imported_ident: ident,
@@ -2441,20 +2436,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24412436
);
24422437

24432438
let mut removal_span = binding_span;
2444-
if found_closing_brace {
2445-
// If the binding span ended with a closing brace, as in the below example:
2446-
// ie. `use a::b::{c, d};`
2447-
// ^
2448-
// Then expand the span of characters to remove to include the previous
2449-
// binding's trailing comma.
2450-
// ie. `use a::b::{c, d};`
2451-
// ^^^
2452-
if let Some(previous_span) =
2439+
2440+
// If the binding span ended with a closing brace, as in the below example:
2441+
// ie. `use a::b::{c, d};`
2442+
// ^
2443+
// Then expand the span of characters to remove to include the previous
2444+
// binding's trailing comma.
2445+
// ie. `use a::b::{c, d};`
2446+
// ^^^
2447+
if found_closing_brace
2448+
&& let Some(previous_span) =
24532449
extend_span_to_previous_binding(self.tcx.sess, binding_span)
2454-
{
2455-
debug!("check_for_module_export_macro: previous_span={:?}", previous_span);
2456-
removal_span = removal_span.with_lo(previous_span.lo());
2457-
}
2450+
{
2451+
debug!("check_for_module_export_macro: previous_span={:?}", previous_span);
2452+
removal_span = removal_span.with_lo(previous_span.lo());
24582453
}
24592454
debug!("check_for_module_export_macro: removal_span={:?}", removal_span);
24602455

0 commit comments

Comments
 (0)