@@ -26,32 +26,50 @@ use syntax::codemap::Span;
26
26
/// out for specific methods where this might not make sense.
27
27
///
28
28
/// **Known problems:** None.
29
+ ///
30
+ /// **Example:**
31
+ /// ```rust
32
+ /// pub fn foo() {} // missing #[inline]
33
+ /// fn ok() {} // ok
34
+ /// #[inline] pub fn bar() {} // ok
35
+ /// #[inline(always)] pub fn baz() {} // ok
36
+ ///
37
+ /// pub trait Bar {
38
+ /// fn bar(); // ok
39
+ /// fn def_bar() {} // missing #[inline]
40
+ /// }
41
+ ///
42
+ /// struct Baz;
43
+ /// impl Baz {
44
+ /// fn priv() {} // ok
45
+ /// }
46
+ ///
47
+ /// impl Bar for Baz {
48
+ /// fn bar() {} // ok - Baz is not exported
49
+ /// }
50
+ ///
51
+ /// pub struct PubBaz;
52
+ /// impl PubBaz {
53
+ /// fn priv() {} // ok
54
+ /// pub not_ptriv() {} // missing #[inline]
55
+ /// }
56
+ ///
57
+ /// impl Bar for PubBaz {
58
+ /// fn bar() {} // missing #[inline]
59
+ /// fn def_bar() {} // missing #[inline]
60
+ /// }
61
+ /// ```
29
62
declare_clippy_lint ! {
30
63
pub MISSING_INLINE_IN_PUBLIC_ITEMS ,
31
64
restriction,
32
65
"detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
33
66
}
34
67
35
- pub struct MissingInline { }
36
-
37
- impl :: std:: default:: Default for MissingInline {
38
- fn default ( ) -> Self {
39
- Self :: new ( )
40
- }
41
- }
68
+ pub struct MissingInline ;
42
69
43
70
impl MissingInline {
44
- pub fn new ( ) -> Self {
45
- Self { }
46
- }
47
-
48
71
fn check_missing_inline_attrs ( & self , cx : & LateContext ,
49
72
attrs : & [ ast:: Attribute ] , sp : Span , desc : & ' static str ) {
50
- // If we're building a test harness, FIXME: is this relevant?
51
- // if cx.sess().opts.test {
52
- // return;
53
- // }
54
-
55
73
let has_inline = attrs
56
74
. iter ( )
57
75
. any ( |a| a. name ( ) == "inline" ) ;
@@ -91,6 +109,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
91
109
} ,
92
110
hir:: ItemTrait ( ref _is_auto, ref _unsafe, ref _generics,
93
111
ref _bounds, ref trait_items) => {
112
+ // note: we need to check if the trait is exported so we can't use
113
+ // `LateLintPass::check_trait_item` here.
94
114
for tit in trait_items {
95
115
let tit_ = cx. tcx . hir . trait_item ( tit. id ) ;
96
116
match tit_. node {
@@ -134,12 +154,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
134
154
return ;
135
155
}
136
156
157
+ let desc = match impl_item. node {
158
+ hir:: ImplItemKind :: Method ( ..) => "a method" ,
159
+ hir:: ImplItemKind :: Const ( ..) |
160
+ hir:: ImplItemKind :: Type ( _) => return ,
161
+ } ;
162
+
137
163
let def_id = cx. tcx . hir . local_def_id ( impl_item. id ) ;
138
164
match cx. tcx . associated_item ( def_id) . container {
139
165
TraitContainer ( cid) => {
140
- let n = cx. tcx . hir . as_local_node_id ( cid) ;
141
- if n. is_some ( ) {
142
- if !cx. access_levels . is_exported ( n. unwrap ( ) ) {
166
+ if let Some ( n) = cx. tcx . hir . as_local_node_id ( cid) {
167
+ if !cx. access_levels . is_exported ( n) {
143
168
// If a trait is being implemented for an item, and the
144
169
// trait is not exported, we don't need #[inline]
145
170
return ;
@@ -149,9 +174,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
149
174
ImplContainer ( cid) => {
150
175
if cx. tcx . impl_trait_ref ( cid) . is_some ( ) {
151
176
let trait_ref = cx. tcx . impl_trait_ref ( cid) . unwrap ( ) ;
152
- let n = cx. tcx . hir . as_local_node_id ( trait_ref. def_id ) ;
153
- if n. is_some ( ) {
154
- if !cx. access_levels . is_exported ( n. unwrap ( ) ) {
177
+ if let Some ( n) = cx. tcx . hir . as_local_node_id ( trait_ref. def_id ) {
178
+ if !cx. access_levels . is_exported ( n) {
155
179
// If a trait is being implemented for an item, and the
156
180
// trait is not exported, we don't need #[inline]
157
181
return ;
@@ -161,11 +185,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
161
185
} ,
162
186
}
163
187
164
- let desc = match impl_item. node {
165
- hir:: ImplItemKind :: Method ( ..) => "a method" ,
166
- hir:: ImplItemKind :: Const ( ..) |
167
- hir:: ImplItemKind :: Type ( _) => return ,
168
- } ;
169
188
self . check_missing_inline_attrs ( cx, & impl_item. attrs , impl_item. span , desc) ;
170
189
}
171
190
}
0 commit comments