diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-03-08 11:04:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-08 11:04:52 +0100 |
| commit | 83ed227a8f66faccade2fa9affc51e50a0087a5b (patch) | |
| tree | e42af7f3d6b86009cf38a7e4c5e1c39707731e23 /compiler | |
| parent | aec535f8051c7cf00b1e373a05893b2cb580eeac (diff) | |
| parent | 700ec66aed90f446a2d09b6fc96599b1f8099057 (diff) | |
| download | rust-83ed227a8f66faccade2fa9affc51e50a0087a5b.tar.gz rust-83ed227a8f66faccade2fa9affc51e50a0087a5b.zip | |
Rollup merge of #94580 - xFrednet:55112-only-reason-in-lint-attr, r=lcnr
Emit `unused_attributes` if a level attr only has a reason Fixes a comment from `compiler/rustc_lint/src/levels.rs`. Lint level attributes that only contain a reason will also trigger the `unused_attribute` lint. The lint now also checks for the `expect` lint level. That's it, have a great rest of the day for everyone reasoning this :upside_down_face: cc: #55112
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_lint/src/levels.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 80 |
2 files changed, 52 insertions, 32 deletions
diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index f46f74fa45f..bbfbf61f486 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -258,7 +258,7 @@ impl<'s> LintLevelsBuilder<'s> { }; if metas.is_empty() { - // FIXME (#55112): issue unused-attributes lint for `#[level()]` + // This emits the unused_attributes lint for `#[level()]` continue; } @@ -271,8 +271,6 @@ impl<'s> LintLevelsBuilder<'s> { ast::MetaItemKind::Word => {} // actual lint names handled later ast::MetaItemKind::NameValue(ref name_value) => { if item.path == sym::reason { - // FIXME (#55112): issue unused-attributes lint if we thereby - // don't have any lint names (`#[level(reason = "foo")]`) if let ast::LitKind::Str(rationale, _) = name_value.kind { if !self.sess.features_untracked().lint_reasons { feature_err( diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index d94ad7ba71a..22020015550 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -4,7 +4,7 @@ //! conflicts between multiple such attributes attached to the same //! item. -use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, NestedMetaItem}; +use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{pluralize, struct_span_err, Applicability}; use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; @@ -178,34 +178,7 @@ impl CheckAttrVisitor<'_> { check_duplicates(self.tcx, attr, hir_id, *duplicates, &mut seen); } - // Warn on useless empty attributes. - if matches!( - attr.name_or_empty(), - sym::macro_use - | sym::allow - | sym::warn - | sym::deny - | sym::forbid - | sym::feature - | sym::repr - | sym::target_feature - ) && attr.meta_item_list().map_or(false, |list| list.is_empty()) - { - self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { - lint.build("unused attribute") - .span_suggestion( - attr.span, - "remove this attribute", - String::new(), - Applicability::MachineApplicable, - ) - .note(&format!( - "attribute `{}` with an empty list has no effect", - attr.name_or_empty() - )) - .emit(); - }); - } + self.check_unused_attribute(hir_id, attr) } if !is_valid { @@ -1969,6 +1942,55 @@ impl CheckAttrVisitor<'_> { }); } } + + fn check_unused_attribute(&self, hir_id: HirId, attr: &Attribute) { + // Warn on useless empty attributes. + let note = if matches!( + attr.name_or_empty(), + sym::macro_use + | sym::allow + | sym::expect + | sym::warn + | sym::deny + | sym::forbid + | sym::feature + | sym::repr + | sym::target_feature + ) && attr.meta_item_list().map_or(false, |list| list.is_empty()) + { + format!( + "attribute `{}` with an empty list has no effect", + attr.name_or_empty() + ) + } else if matches!( + attr.name_or_empty(), + sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect + ) && let Some(meta) = attr.meta_item_list() + && meta.len() == 1 + && let Some(item) = meta[0].meta_item() + && let MetaItemKind::NameValue(_) = &item.kind + && item.path == sym::reason + { + format!( + "attribute `{}` without any lints has no effect", + attr.name_or_empty() + ) + } else { + return; + }; + + self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { + lint.build("unused attribute") + .span_suggestion( + attr.span, + "remove this attribute", + String::new(), + Applicability::MachineApplicable, + ) + .note(¬e) + .emit(); + }); + } } impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { |
