Skip to content

Lint for trait methods without bodies #2367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ All notable changes to this project will be documented in this file.
[`ineffective_bit_mask`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#ineffective_bit_mask
[`infinite_iter`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#infinite_iter
[`inline_always`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#inline_always
[`inline_fn_without_body`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#inline_fn_without_body
[`int_plus_one`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#int_plus_one
[`integer_arithmetic`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#integer_arithmetic
[`invalid_ref`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#invalid_ref
Expand Down
65 changes: 65 additions & 0 deletions clippy_lints/src/inline_fn_without_body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! checks for `#[inline]` on trait methods without bodies

use rustc::lint::*;
use rustc::hir::*;
use syntax::ast::{Attribute, Name};
use utils::span_lint_and_then;
use utils::sugg::DiagnosticBuilderExt;

/// **What it does:** Checks for `#[inline]` on trait methods without bodies
///
/// **Why is this bad?** Only implementations of trait methods may be inlined.
/// The inline attribute is ignored for trait methods without bodies.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// trait Animal {
/// #[inline]
/// fn name(&self) -> &'static str;
/// }
/// ```
declare_lint! {
pub INLINE_FN_WITHOUT_BODY,
Warn,
"use of `#[inline]` on trait methods without bodies"
}

#[derive(Copy, Clone)]
pub struct Pass;

impl LintPass for Pass {
fn get_lints(&self) -> LintArray {
lint_array!(INLINE_FN_WITHOUT_BODY)
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
match item.node {
TraitItemKind::Method(_, TraitMethod::Required(_)) => {
check_attrs(cx, &item.name, &item.attrs);
},
_ => {},
}
}
}

fn check_attrs(cx: &LateContext, name: &Name, attrs: &[Attribute]) {
for attr in attrs {
if attr.name().map_or(true, |n| n != "inline") {
continue;
}

span_lint_and_then(
cx,
INLINE_FN_WITHOUT_BODY,
attr.span,
&format!("use of `#[inline]` on trait method `{}` which has no body", name),
|db| {
db.suggest_remove_item(cx, attr.span, "remove");
},
);
}
}
3 changes: 3 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub mod identity_op;
pub mod if_let_redundant_pattern_matching;
pub mod if_not_else;
pub mod infinite_iter;
pub mod inline_fn_without_body;
pub mod int_plus_one;
pub mod invalid_ref;
pub mod is_unit_expr;
Expand Down Expand Up @@ -359,6 +360,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
reg.register_late_lint_pass(box use_self::UseSelf);
reg.register_late_lint_pass(box bytecount::ByteCount);
reg.register_late_lint_pass(box infinite_iter::Pass);
reg.register_late_lint_pass(box inline_fn_without_body::Pass);
reg.register_late_lint_pass(box invalid_ref::InvalidRef);
reg.register_late_lint_pass(box identity_conversion::IdentityConversion::default());
reg.register_late_lint_pass(box types::ImplicitHasher);
Expand Down Expand Up @@ -477,6 +479,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
identity_op::IDENTITY_OP,
if_let_redundant_pattern_matching::IF_LET_REDUNDANT_PATTERN_MATCHING,
infinite_iter::INFINITE_ITER,
inline_fn_without_body::INLINE_FN_WITHOUT_BODY,
invalid_ref::INVALID_REF,
is_unit_expr::UNIT_EXPR,
large_enum_variant::LARGE_ENUM_VARIANT,
Expand Down
31 changes: 31 additions & 0 deletions clippy_lints/src/utils/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use syntax::print::pprust::token_to_string;
use syntax::util::parser::AssocOp;
use syntax::ast;
use utils::{higher, snippet, snippet_opt};
use syntax_pos::{BytePos, Pos};

/// A helper type to build suggestion correctly handling parenthesis.
pub enum Sugg<'a> {
Expand Down Expand Up @@ -454,6 +455,19 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
/// }");
/// ```
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str);

/// Suggest to completely remove an item.
///
/// This will remove an item and all following whitespace until the next non-whitespace
/// character. This should work correctly if item is on the same indentation level as the
/// following item.
///
/// # Example
///
/// ```rust,ignore
/// db.suggest_remove_item(cx, item, "remove this")
/// ```
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str);
}

impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> {
Expand Down Expand Up @@ -485,4 +499,21 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
self.span_suggestion(span, msg, format!("{}\n{}", new_item, indent));
}
}

fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str) {
let mut remove_span = item;
let fmpos = cx.sess()
.codemap()
.lookup_byte_offset(remove_span.next_point().hi());

if let Some(ref src) = fmpos.fm.src {
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');

if let Some(non_whitespace_offset) = non_whitespace_offset {
remove_span = remove_span.with_hi(remove_span.hi() + BytePos(non_whitespace_offset as u32))
}
}

self.span_suggestion(remove_span, msg, String::new());
}
}
Binary file added main
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/ui/inline_fn_without_body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@



#![warn(inline_fn_without_body)]
#![allow(inline_always)]

trait Foo {
#[inline]
fn default_inline();

#[inline(always)]fn always_inline();

#[inline(never)]

fn never_inline();

#[inline]
fn has_body() {
}
}

fn main() {
}
27 changes: 27 additions & 0 deletions tests/ui/inline_fn_without_body.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: use of `#[inline]` on trait method `default_inline` which has no body
--> $DIR/inline_fn_without_body.rs:8:5
|
8 | #[inline]
| _____-^^^^^^^^
9 | | fn default_inline();
| |____- help: remove
|
= note: `-D inline-fn-without-body` implied by `-D warnings`

error: use of `#[inline]` on trait method `always_inline` which has no body
--> $DIR/inline_fn_without_body.rs:11:5
|
11 | #[inline(always)]fn always_inline();
| ^^^^^^^^^^^^^^^^^ help: remove

error: use of `#[inline]` on trait method `never_inline` which has no body
--> $DIR/inline_fn_without_body.rs:13:5
|
13 | #[inline(never)]
| _____-^^^^^^^^^^^^^^^
14 | |
15 | | fn never_inline();
| |____- help: remove

error: aborting due to 3 previous errors