Skip to content

Commit 94c4dd9

Browse files
committed
Emit warning for ignored #[inline] on trait method prototypes
1 parent 7f3c843 commit 94c4dd9

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

src/librustc/hir/check_attr.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::hir::{self, HirId, HirVec, Attribute, Item, ItemKind, TraitItem, Trai
88
use crate::hir::DUMMY_HIR_ID;
99
use crate::hir::def_id::DefId;
1010
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
11+
use crate::lint::builtin::UNUSED_ATTRIBUTES;
1112
use crate::ty::TyCtxt;
1213
use crate::ty::query::Providers;
1314

@@ -36,6 +37,9 @@ pub(crate) enum Target {
3637
Impl,
3738
Expression,
3839
Statement,
40+
AssocConst,
41+
Method { body: bool },
42+
AssocTy,
3943
}
4044

4145
impl Display for Target {
@@ -60,6 +64,9 @@ impl Display for Target {
6064
Target::Impl => "item",
6165
Target::Expression => "expression",
6266
Target::Statement => "statement",
67+
Target::AssocConst => "associated const",
68+
Target::Method { .. } => "method",
69+
Target::AssocTy => "associated type",
6370
})
6471
}
6572
}
@@ -85,6 +92,19 @@ impl Target {
8592
ItemKind::Impl(..) => Target::Impl,
8693
}
8794
}
95+
96+
fn from_trait_item(trait_item: &TraitItem) -> Target {
97+
match trait_item.kind {
98+
TraitItemKind::Const(..) => Target::AssocConst,
99+
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
100+
Target::Method { body: false }
101+
}
102+
TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
103+
Target::Method { body: true }
104+
}
105+
TraitItemKind::Type(..) => Target::AssocTy,
106+
}
107+
}
88108
}
89109
}
90110
}
@@ -136,6 +156,15 @@ impl CheckAttrVisitor<'tcx> {
136156
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
137157
match target {
138158
Target::Fn | Target::Closure | Target::Method { body: true } => true,
159+
Target::Method { body: false } | Target::ForeignFn => {
160+
self.tcx.struct_span_lint_hir(
161+
UNUSED_ATTRIBUTES,
162+
hir_id,
163+
attr.span,
164+
"`#[inline]` is ignored on function prototypes",
165+
).emit();
166+
true
167+
}
139168
_ => {
140169
struct_span_err!(self.tcx.sess,
141170
attr.span,
@@ -392,6 +421,11 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
392421
intravisit::walk_item(self, item)
393422
}
394423

424+
fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem) {
425+
let target = Target::from_trait_item(trait_item);
426+
self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
427+
intravisit::walk_trait_item(self, trait_item)
428+
}
395429

396430
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
397431
self.check_stmt_attributes(stmt);

src/librustc/lint/builtin.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ declare_lint! {
6868
"detect unused, unexported items"
6969
}
7070

71+
declare_lint! {
72+
pub UNUSED_ATTRIBUTES,
73+
Warn,
74+
"detects attributes that were not used by the compiler"
75+
}
76+
7177
declare_lint! {
7278
pub UNREACHABLE_CODE,
7379
Warn,

src/librustc_lint/unused.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc::hir::def::{Res, DefKind};
22
use rustc::hir::def_id::DefId;
33
use rustc::lint;
4+
use rustc::lint::builtin::UNUSED_ATTRIBUTES;
45
use rustc::ty::{self, Ty};
56
use rustc::ty::adjustment;
67
use rustc_data_structures::fx::FxHashMap;
@@ -277,12 +278,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
277278
}
278279
}
279280

280-
declare_lint! {
281-
pub UNUSED_ATTRIBUTES,
282-
Warn,
283-
"detects attributes that were not used by the compiler"
284-
}
285-
286281
#[derive(Copy, Clone)]
287282
pub struct UnusedAttributes {
288283
builtin_attributes: &'static FxHashMap<Symbol, &'static BuiltinAttribute>,

src/test/ui/issues/issue-52057.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: `#[inline]` is ignored on function prototypes
2+
--> $DIR/issue-52057.rs:10:5
3+
|
4+
LL | #[inline(always)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(unused_attributes)]` on by default
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![deny(unused_attributes)]
2+
3+
trait Trait {
4+
#[inline] //~ ERROR `#[inline]` is ignored on function prototypes
5+
fn foo();
6+
}
7+
8+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: `#[inline]` is ignored on function prototypes
2+
--> $DIR/warn-unused-inline-on-fn-prototypes.rs:9:5
3+
|
4+
LL | #[inline]
5+
| ^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/warn-unused-inline-on-fn-prototypes.rs:1:9
9+
|
10+
LL | #![deny(unused_attributes)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)