Auto merge of #138165 - jdonszelmann:inline, r=oli-obk
Rewrite `inline` attribute parser to use new infrastructure and improve diagnostics for all parsed attributes r? `@oli-obk` This PR: - creates a new parser for inline attributes - creates consistent error messages and error codes between attribute parsers; inline and others - as such changes a few error messages for other attributes to be (in my eyes) much more consistent - tests ast-lowering lints introduced by rust-lang/rust#138164 since this is now useful for the first time - Coalesce some useless error codes Builds on top of rust-lang/rust#138164 Closes rust-lang/rust#137950
This commit is contained in:
commit
76d583ba0e
4 changed files with 28 additions and 29 deletions
|
|
@ -1,29 +1,22 @@
|
|||
use super::INLINE_ALWAYS;
|
||||
use super::utils::is_word;
|
||||
use clippy_utils::diagnostics::span_lint;
|
||||
use rustc_attr_data_structures::{find_attr, AttributeKind, InlineAttr};
|
||||
use rustc_hir::Attribute;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_span::symbol::Symbol;
|
||||
use rustc_span::{Span, sym};
|
||||
use rustc_span::Span;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribute]) {
|
||||
if span.from_expansion() {
|
||||
return;
|
||||
}
|
||||
|
||||
for attr in attrs {
|
||||
if let Some(values) = attr.meta_item_list() {
|
||||
if values.len() != 1 || !attr.has_name(sym::inline) {
|
||||
continue;
|
||||
}
|
||||
if is_word(&values[0], sym::always) {
|
||||
span_lint(
|
||||
cx,
|
||||
INLINE_ALWAYS,
|
||||
attr.span(),
|
||||
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(span) = find_attr!(attrs, AttributeKind::Inline(InlineAttr::Always, span) => *span) {
|
||||
span_lint(
|
||||
cx,
|
||||
INLINE_ALWAYS,
|
||||
span,
|
||||
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::sugg::DiagExt;
|
||||
use rustc_attr_data_structures::{find_attr, AttributeKind};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::sym;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
|
|
@ -32,15 +32,19 @@ declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
|
|||
impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
|
||||
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind
|
||||
&& let Some(attr) = cx.tcx.hir_attrs(item.hir_id()).iter().find(|a| a.has_name(sym::inline))
|
||||
&& let Some(attr_span) = find_attr!(cx
|
||||
.tcx
|
||||
.hir_attrs(item.hir_id()),
|
||||
AttributeKind::Inline(_, span) => *span
|
||||
)
|
||||
{
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
INLINE_FN_WITHOUT_BODY,
|
||||
attr.span(),
|
||||
attr_span,
|
||||
format!("use of `#[inline]` on trait method `{}` which has no body", item.ident),
|
||||
|diag| {
|
||||
diag.suggest_remove_item(cx, attr.span(), "remove", Applicability::MachineApplicable);
|
||||
diag.suggest_remove_item(cx, attr_span, "remove", Applicability::MachineApplicable);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
use clippy_utils::diagnostics::span_lint;
|
||||
use rustc_attr_data_structures::{find_attr, AttributeKind};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::Attribute;
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::ty::AssocItemContainer;
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::{Span, sym};
|
||||
use rustc_span::Span;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
|
|
@ -64,8 +65,7 @@ declare_clippy_lint! {
|
|||
}
|
||||
|
||||
fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[Attribute], sp: Span, desc: &'static str) {
|
||||
let has_inline = attrs.iter().any(|a| a.has_name(sym::inline));
|
||||
if !has_inline {
|
||||
if !find_attr!(attrs, AttributeKind::Inline(..)) {
|
||||
span_lint(
|
||||
cx,
|
||||
MISSING_INLINE_IN_PUBLIC_ITEMS,
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
|
|||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::ty::{for_each_top_level_late_bound_region, is_copy};
|
||||
use clippy_utils::{is_self, is_self_ty};
|
||||
use rustc_attr_data_structures::{find_attr, AttributeKind, InlineAttr};
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use core::ops::ControlFlow;
|
||||
use rustc_abi::ExternAbi;
|
||||
use rustc_ast::attr;
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::intravisit::FnKind;
|
||||
|
|
@ -270,11 +270,13 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
|
|||
return;
|
||||
}
|
||||
let attrs = cx.tcx.hir_attrs(hir_id);
|
||||
if find_attr!(attrs, AttributeKind::Inline(InlineAttr::Always, _)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for a in attrs {
|
||||
if let Some(meta_items) = a.meta_item_list()
|
||||
&& (a.has_name(sym::proc_macro_derive)
|
||||
|| (a.has_name(sym::inline) && attr::list_contains_name(&meta_items, sym::always)))
|
||||
{
|
||||
// FIXME(jdonszelmann): make part of the find_attr above
|
||||
if a.has_name(sym::proc_macro_derive) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue