Skip to content

Commit 999a00b

Browse files
committed
address reviews
1 parent 7c4ec40 commit 999a00b

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
365365
reg.register_late_lint_pass(box let_if_seq::LetIfSeq);
366366
reg.register_late_lint_pass(box eval_order_dependence::EvalOrderDependence);
367367
reg.register_late_lint_pass(box missing_doc::MissingDoc::new());
368-
reg.register_late_lint_pass(box missing_inline::MissingInline::new());
368+
reg.register_late_lint_pass(box missing_inline::MissingInline);
369369
reg.register_late_lint_pass(box ok_if_let::Pass);
370370
reg.register_late_lint_pass(box if_let_redundant_pattern_matching::Pass);
371371
reg.register_late_lint_pass(box partialeq_ne_impl::Pass);

clippy_lints/src/missing_inline.rs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,50 @@ use syntax::codemap::Span;
2626
/// out for specific methods where this might not make sense.
2727
///
2828
/// **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+
/// ```
2962
declare_clippy_lint! {
3063
pub MISSING_INLINE_IN_PUBLIC_ITEMS,
3164
restriction,
3265
"detects missing #[inline] attribute for public callables (functions, trait methods, methods...)"
3366
}
3467

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;
4269

4370
impl MissingInline {
44-
pub fn new() -> Self {
45-
Self {}
46-
}
47-
4871
fn check_missing_inline_attrs(&self, cx: &LateContext,
4972
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-
5573
let has_inline = attrs
5674
.iter()
5775
.any(|a| a.name() == "inline" );
@@ -91,6 +109,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
91109
},
92110
hir::ItemTrait(ref _is_auto, ref _unsafe, ref _generics,
93111
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.
94114
for tit in trait_items {
95115
let tit_ = cx.tcx.hir.trait_item(tit.id);
96116
match tit_.node {
@@ -134,12 +154,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
134154
return;
135155
}
136156

157+
let desc = match impl_item.node {
158+
hir::ImplItemKind::Method(..) => "a method",
159+
hir::ImplItemKind::Const(..) |
160+
hir::ImplItemKind::Type(_) => return,
161+
};
162+
137163
let def_id = cx.tcx.hir.local_def_id(impl_item.id);
138164
match cx.tcx.associated_item(def_id).container {
139165
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) {
143168
// If a trait is being implemented for an item, and the
144169
// trait is not exported, we don't need #[inline]
145170
return;
@@ -149,9 +174,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
149174
ImplContainer(cid) => {
150175
if cx.tcx.impl_trait_ref(cid).is_some() {
151176
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) {
155179
// If a trait is being implemented for an item, and the
156180
// trait is not exported, we don't need #[inline]
157181
return;
@@ -161,11 +185,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline {
161185
},
162186
}
163187

164-
let desc = match impl_item.node {
165-
hir::ImplItemKind::Method(..) => "a method",
166-
hir::ImplItemKind::Const(..) |
167-
hir::ImplItemKind::Type(_) => return,
168-
};
169188
self.check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
170189
}
171190
}

0 commit comments

Comments
 (0)