Skip to content

Commit cf91a93

Browse files
committed
rustc_resolve: flatten nested ifs
1 parent a93616a commit cf91a93

File tree

5 files changed

+198
-229
lines changed

5 files changed

+198
-229
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
645645
let self_spans = items
646646
.iter()
647647
.filter_map(|(use_tree, _)| {
648-
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
649-
if use_tree.ident().name == kw::SelfLower {
650-
return Some(use_tree.span);
651-
}
648+
if let ast::UseTreeKind::Simple(..) = use_tree.kind
649+
&& use_tree.ident().name == kw::SelfLower
650+
{
651+
return Some(use_tree.span);
652652
}
653653

654654
None
@@ -947,19 +947,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
947947
let imported_binding = self.r.import(binding, import);
948948
if parent == self.r.graph_root {
949949
let ident = ident.normalize_to_macros_2_0();
950-
if let Some(entry) = self.r.extern_prelude.get(&ident) {
951-
if expansion != LocalExpnId::ROOT && orig_name.is_some() && !entry.is_import() {
952-
self.r.dcx().emit_err(
953-
errors::MacroExpandedExternCrateCannotShadowExternArguments {
954-
span: item.span,
955-
},
956-
);
957-
// `return` is intended to discard this binding because it's an
958-
// unregistered ambiguity error which would result in a panic
959-
// caused by inconsistency `path_res`
960-
// more details: https://github.com/rust-lang/rust/pull/111761
961-
return;
962-
}
950+
if let Some(entry) = self.r.extern_prelude.get(&ident)
951+
&& expansion != LocalExpnId::ROOT
952+
&& orig_name.is_some()
953+
&& !entry.is_import()
954+
{
955+
self.r.dcx().emit_err(
956+
errors::MacroExpandedExternCrateCannotShadowExternArguments { span: item.span },
957+
);
958+
// `return` is intended to discard this binding because it's an
959+
// unregistered ambiguity error which would result in a panic
960+
// caused by inconsistency `path_res`
961+
// more details: https://github.com/rust-lang/rust/pull/111761
962+
return;
963963
}
964964
let entry = self
965965
.r
@@ -1040,10 +1040,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10401040
span: item.span,
10411041
});
10421042
}
1043-
if let ItemKind::ExternCrate(Some(orig_name)) = item.kind {
1044-
if orig_name == kw::SelfLower {
1045-
self.r.dcx().emit_err(errors::MacroUseExternCrateSelf { span: attr.span });
1046-
}
1043+
if let ItemKind::ExternCrate(Some(orig_name)) = item.kind
1044+
&& orig_name == kw::SelfLower
1045+
{
1046+
self.r.dcx().emit_err(errors::MacroUseExternCrateSelf { span: attr.span });
10471047
}
10481048
let ill_formed = |span| {
10491049
self.r.dcx().emit_err(errors::BadMacroImport { span });
@@ -1179,14 +1179,12 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11791179
return Some((MacroKind::Bang, item.ident, item.span));
11801180
} else if ast::attr::contains_name(&item.attrs, sym::proc_macro_attribute) {
11811181
return Some((MacroKind::Attr, item.ident, item.span));
1182-
} else if let Some(attr) = ast::attr::find_by_name(&item.attrs, sym::proc_macro_derive) {
1183-
if let Some(meta_item_inner) =
1182+
} else if let Some(attr) = ast::attr::find_by_name(&item.attrs, sym::proc_macro_derive)
1183+
&& let Some(meta_item_inner) =
11841184
attr.meta_item_list().and_then(|list| list.get(0).cloned())
1185-
{
1186-
if let Some(ident) = meta_item_inner.ident() {
1187-
return Some((MacroKind::Derive, ident, ident.span));
1188-
}
1189-
}
1185+
&& let Some(ident) = meta_item_inner.ident()
1186+
{
1187+
return Some((MacroKind::Derive, ident, ident.span));
11901188
}
11911189
None
11921190
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
225225
let (name, span) =
226226
(ident.name, self.tcx.sess.source_map().guess_head_span(new_binding.span));
227227

228-
if let Some(s) = self.name_already_seen.get(&name) {
229-
if s == &span {
230-
return;
231-
}
228+
if self.name_already_seen.get(&name) == Some(&span) {
229+
return;
232230
}
233231

234232
let old_kind = match (ns, old_binding.module()) {
@@ -380,20 +378,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
380378
suggestion = Some(format!("self as {suggested_name}"))
381379
}
382380
ImportKind::Single { source, .. } => {
383-
if let Some(pos) =
384-
source.span.hi().0.checked_sub(binding_span.lo().0).map(|pos| pos as usize)
381+
if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
382+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span)
383+
&& pos as usize <= snippet.len()
385384
{
386-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span) {
387-
if pos <= snippet.len() {
388-
span = binding_span
389-
.with_lo(binding_span.lo() + BytePos(pos as u32))
390-
.with_hi(
391-
binding_span.hi()
392-
- BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
393-
);
394-
suggestion = Some(format!(" as {suggested_name}"));
395-
}
396-
}
385+
span = binding_span.with_lo(binding_span.lo() + BytePos(pos)).with_hi(
386+
binding_span.hi() - BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
387+
);
388+
suggestion = Some(format!(" as {suggested_name}"));
397389
}
398390
}
399391
ImportKind::ExternCrate { source, target, .. } => {
@@ -510,13 +502,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
510502
// If the first element of our path was actually resolved to an
511503
// `ExternCrate` (also used for `crate::...`) then no need to issue a
512504
// warning, this looks all good!
513-
if let Some(binding) = second_binding {
514-
if let NameBindingKind::Import { import, .. } = binding.kind {
515-
// Careful: we still want to rewrite paths from renamed extern crates.
516-
if let ImportKind::ExternCrate { source: None, .. } = import.kind {
517-
return;
518-
}
519-
}
505+
if let Some(binding) = second_binding
506+
&& let NameBindingKind::Import { import, .. } = binding.kind
507+
// Careful: we still want to rewrite paths from renamed extern crates.
508+
&& let ImportKind::ExternCrate { source: None, .. } = import.kind
509+
{
510+
return;
520511
}
521512

522513
let diag = BuiltinLintDiag::AbsPathWithModule(root_span);
@@ -1215,12 +1206,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12151206
}
12161207

12171208
// #90113: Do not count an inaccessible reexported item as a candidate.
1218-
if let NameBindingKind::Import { binding, .. } = name_binding.kind {
1219-
if this.is_accessible_from(binding.vis, parent_scope.module)
1220-
&& !this.is_accessible_from(name_binding.vis, parent_scope.module)
1221-
{
1222-
return;
1223-
}
1209+
if let NameBindingKind::Import { binding, .. } = name_binding.kind
1210+
&& this.is_accessible_from(binding.vis, parent_scope.module)
1211+
&& !this.is_accessible_from(name_binding.vis, parent_scope.module)
1212+
{
1213+
return;
12241214
}
12251215

12261216
let res = name_binding.res();
@@ -1253,14 +1243,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12531243
segms.push(ast::PathSegment::from_ident(ident));
12541244
let path = Path { span: name_binding.span, segments: segms, tokens: None };
12551245

1256-
if child_accessible {
1246+
if child_accessible
12571247
// Remove invisible match if exists
1258-
if let Some(idx) = candidates
1248+
&& let Some(idx) = candidates
12591249
.iter()
12601250
.position(|v: &ImportSuggestion| v.did == did && !v.accessible)
1261-
{
1262-
candidates.remove(idx);
1263-
}
1251+
{
1252+
candidates.remove(idx);
12641253
}
12651254

12661255
if candidates.iter().all(|v: &ImportSuggestion| v.did != did) {
@@ -1545,19 +1534,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15451534
macro_kind.descr_expected(),
15461535
),
15471536
};
1548-
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
1549-
if !import.span.is_dummy() {
1550-
let note = errors::IdentImporterHereButItIsDesc {
1551-
span: import.span,
1552-
imported_ident: ident,
1553-
imported_ident_desc: &desc,
1554-
};
1555-
err.subdiagnostic(note);
1556-
// Silence the 'unused import' warning we might get,
1557-
// since this diagnostic already covers that import.
1558-
self.record_use(ident, binding, Used::Other);
1559-
return;
1560-
}
1537+
if let crate::NameBindingKind::Import { import, .. } = binding.kind
1538+
&& !import.span.is_dummy()
1539+
{
1540+
let note = errors::IdentImporterHereButItIsDesc {
1541+
span: import.span,
1542+
imported_ident: ident,
1543+
imported_ident_desc: &desc,
1544+
};
1545+
err.subdiagnostic(note);
1546+
// Silence the 'unused import' warning we might get,
1547+
// since this diagnostic already covers that import.
1548+
self.record_use(ident, binding, Used::Other);
1549+
return;
15611550
}
15621551
let note = errors::IdentInScopeButItIsDesc {
15631552
imported_ident: ident,
@@ -2436,20 +2425,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24362425
debug!(found_closing_brace, ?binding_span);
24372426

24382427
let mut removal_span = binding_span;
2439-
if found_closing_brace {
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 let Some(previous_span) =
2428+
2429+
// If the binding span ended with a closing brace, as in the below example:
2430+
// ie. `use a::b::{c, d};`
2431+
// ^
2432+
// Then expand the span of characters to remove to include the previous
2433+
// binding's trailing comma.
2434+
// ie. `use a::b::{c, d};`
2435+
// ^^^
2436+
if found_closing_brace
2437+
&& let Some(previous_span) =
24482438
extend_span_to_previous_binding(self.tcx.sess, binding_span)
2449-
{
2450-
debug!(?previous_span);
2451-
removal_span = removal_span.with_lo(previous_span.lo());
2452-
}
2439+
{
2440+
debug!(?previous_span);
2441+
removal_span = removal_span.with_lo(previous_span.lo());
24532442
}
24542443
debug!(?removal_span);
24552444

@@ -3064,16 +3053,16 @@ impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
30643053

30653054
fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
30663055
for item in items {
3067-
if let ItemKind::Use(..) = item.kind {
3068-
if is_span_suitable_for_use_injection(item.span) {
3069-
let mut lo = item.span.lo();
3070-
for attr in &item.attrs {
3071-
if attr.span.eq_ctxt(item.span) {
3072-
lo = std::cmp::min(lo, attr.span.lo());
3073-
}
3056+
if let ItemKind::Use(..) = item.kind
3057+
&& is_span_suitable_for_use_injection(item.span)
3058+
{
3059+
let mut lo = item.span.lo();
3060+
for attr in &item.attrs {
3061+
if attr.span.eq_ctxt(item.span) {
3062+
lo = std::cmp::min(lo, attr.span.lo());
30743063
}
3075-
return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent()));
30763064
}
3065+
return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent()));
30773066
}
30783067
}
30793068
None

0 commit comments

Comments
 (0)