Skip to content

Commit 55e7818

Browse files
committed
Auto merge of #4619 - james9909:unused-self, r=flip1995
Add a lint for unused self changelog: Adds a new lint: `unused_self` Closes #4550.
2 parents 8fae2dd + adf5886 commit 55e7818

19 files changed

+373
-83
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ Released 2018-09-13
12361236
[`unused_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_collect
12371237
[`unused_io_amount`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount
12381238
[`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label
1239+
[`unused_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_self
12391240
[`unused_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
12401241
[`use_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_debug
12411242
[`use_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_self

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 324 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 325 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/double_comparison.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare_lint_pass!(DoubleComparisons => [DOUBLE_COMPARISONS]);
3939

4040
impl<'a, 'tcx> DoubleComparisons {
4141
#[allow(clippy::similar_names)]
42-
fn check_binop(self, cx: &LateContext<'a, 'tcx>, op: BinOpKind, lhs: &'tcx Expr, rhs: &'tcx Expr, span: Span) {
42+
fn check_binop(cx: &LateContext<'a, 'tcx>, op: BinOpKind, lhs: &'tcx Expr, rhs: &'tcx Expr, span: Span) {
4343
let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (&lhs.kind, &rhs.kind) {
4444
(ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => {
4545
(lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
@@ -89,7 +89,7 @@ impl<'a, 'tcx> DoubleComparisons {
8989
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DoubleComparisons {
9090
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
9191
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = expr.kind {
92-
self.check_binop(cx, kind.node, lhs, rhs, expr.span);
92+
Self::check_binop(cx, kind.node, lhs, rhs, expr.span);
9393
}
9494
}
9595
}

clippy_lints/src/enum_glob_use.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
3232
let map = cx.tcx.hir();
3333
// only check top level `use` statements
3434
for item in &m.item_ids {
35-
self.lint_item(cx, map.expect_item(item.id));
35+
lint_item(cx, map.expect_item(item.id));
3636
}
3737
}
3838
}
3939

40-
impl EnumGlobUse {
41-
fn lint_item(self, cx: &LateContext<'_, '_>, item: &Item) {
42-
if item.vis.node.is_pub() {
43-
return; // re-exports are fine
44-
}
45-
if let ItemKind::Use(ref path, UseKind::Glob) = item.kind {
46-
if let Res::Def(DefKind::Enum, _) = path.res {
47-
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
48-
}
40+
fn lint_item(cx: &LateContext<'_, '_>, item: &Item) {
41+
if item.vis.node.is_pub() {
42+
return; // re-exports are fine
43+
}
44+
if let ItemKind::Use(ref path, UseKind::Glob) = item.kind {
45+
if let Res::Def(DefKind::Enum, _) = path.res {
46+
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
4947
}
5048
}
5149
}

clippy_lints/src/excessive_precision.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
4444
if let ty::Float(fty) = ty.kind;
4545
if let hir::ExprKind::Lit(ref lit) = expr.kind;
4646
if let LitKind::Float(sym, _) | LitKind::FloatUnsuffixed(sym) = lit.node;
47-
if let Some(sugg) = self.check(sym, fty);
47+
if let Some(sugg) = Self::check(sym, fty);
4848
then {
4949
span_lint_and_sugg(
5050
cx,
@@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
6363
impl ExcessivePrecision {
6464
// None if nothing to lint, Some(suggestion) if lint necessary
6565
#[must_use]
66-
fn check(self, sym: Symbol, fty: FloatTy) -> Option<String> {
66+
fn check(sym: Symbol, fty: FloatTy) -> Option<String> {
6767
let max = max_digits(fty);
6868
let sym_str = sym.as_str();
6969
if dot_zero_exclusion(&sym_str) {

clippy_lints/src/functions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
222222
}
223223
}
224224

225-
self.check_raw_ptr(cx, unsafety, decl, body, hir_id);
225+
Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
226226
self.check_line_number(cx, span, body);
227227
}
228228

@@ -282,7 +282,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
282282
}
283283
if let hir::TraitMethod::Provided(eid) = *eid {
284284
let body = cx.tcx.hir().body(eid);
285-
self.check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
285+
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
286286

287287
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) {
288288
check_must_use_candidate(
@@ -368,7 +368,6 @@ impl<'a, 'tcx> Functions {
368368
}
369369

370370
fn check_raw_ptr(
371-
self,
372371
cx: &LateContext<'a, 'tcx>,
373372
unsafety: hir::Unsafety,
374373
decl: &'tcx hir::FnDecl,

clippy_lints/src/int_plus_one.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ enum Side {
5353

5454
impl IntPlusOne {
5555
#[allow(clippy::cast_sign_loss)]
56-
fn check_lit(self, lit: &Lit, target_value: i128) -> bool {
56+
fn check_lit(lit: &Lit, target_value: i128) -> bool {
5757
if let LitKind::Int(value, ..) = lit.kind {
5858
return value == (target_value as u128);
5959
}
6060
false
6161
}
6262

63-
fn check_binop(self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
63+
fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
6464
match (binop, &lhs.kind, &rhs.kind) {
6565
// case where `x - 1 >= ...` or `-1 + x >= ...`
6666
(BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
6767
match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
6868
// `-1 + x`
69-
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
70-
self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
69+
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
70+
Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
7171
},
7272
// `x - 1`
73-
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
74-
self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
73+
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
74+
Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
7575
},
7676
_ => None,
7777
}
@@ -82,11 +82,11 @@ impl IntPlusOne {
8282
{
8383
match (&rhslhs.kind, &rhsrhs.kind) {
8484
// `y + 1` and `1 + y`
85-
(&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
86-
self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
85+
(&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
86+
Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
8787
},
88-
(_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
89-
self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
88+
(_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
89+
Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
9090
},
9191
_ => None,
9292
}
@@ -97,11 +97,11 @@ impl IntPlusOne {
9797
{
9898
match (&lhslhs.kind, &lhsrhs.kind) {
9999
// `1 + x` and `x + 1`
100-
(&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
101-
self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
100+
(&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
101+
Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
102102
},
103-
(_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
104-
self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
103+
(_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
104+
Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
105105
},
106106
_ => None,
107107
}
@@ -110,12 +110,12 @@ impl IntPlusOne {
110110
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
111111
match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
112112
// `-1 + y`
113-
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
114-
self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
113+
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
114+
Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
115115
},
116116
// `y - 1`
117-
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
118-
self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
117+
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
118+
Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
119119
},
120120
_ => None,
121121
}
@@ -125,7 +125,6 @@ impl IntPlusOne {
125125
}
126126

127127
fn generate_recommendation(
128-
self,
129128
cx: &EarlyContext<'_>,
130129
binop: BinOpKind,
131130
node: &Expr,
@@ -149,7 +148,7 @@ impl IntPlusOne {
149148
None
150149
}
151150

152-
fn emit_warning(self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
151+
fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
153152
span_lint_and_then(
154153
cx,
155154
INT_PLUS_ONE,
@@ -170,8 +169,8 @@ impl IntPlusOne {
170169
impl EarlyLintPass for IntPlusOne {
171170
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
172171
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
173-
if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
174-
self.emit_warning(cx, item, rec.clone());
172+
if let Some(ref rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
173+
Self::emit_warning(cx, item, rec.clone());
175174
}
176175
}
177176
}

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ pub mod unicode;
277277
pub mod unsafe_removed_from_name;
278278
pub mod unused_io_amount;
279279
pub mod unused_label;
280+
pub mod unused_self;
280281
pub mod unwrap;
281282
pub mod use_self;
282283
pub mod vec;
@@ -606,6 +607,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
606607
reg.register_late_lint_pass(box trait_bounds::TraitBounds);
607608
reg.register_late_lint_pass(box comparison_chain::ComparisonChain);
608609
reg.register_late_lint_pass(box mul_add::MulAddCheck);
610+
reg.register_late_lint_pass(box unused_self::UnusedSelf);
609611

610612
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
611613
arithmetic::FLOAT_ARITHMETIC,
@@ -683,6 +685,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
683685
types::LINKEDLIST,
684686
unicode::NON_ASCII_LITERAL,
685687
unicode::UNICODE_NOT_NFC,
688+
unused_self::UNUSED_SELF,
686689
use_self::USE_SELF,
687690
]);
688691

clippy_lints/src/literal_representation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,13 @@ impl EarlyLintPass for LiteralDigitGrouping {
350350
}
351351

352352
if let ExprKind::Lit(ref lit) = expr.kind {
353-
self.check_lit(cx, lit)
353+
Self::check_lit(cx, lit)
354354
}
355355
}
356356
}
357357

358358
impl LiteralDigitGrouping {
359-
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
359+
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
360360
let in_macro = in_macro(lit.span);
361361
match lit.kind {
362362
LitKind::Int(..) => {

clippy_lints/src/misc_early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl EarlyLintPass for MiscEarlyLints {
437437
);
438438
}
439439
},
440-
ExprKind::Lit(ref lit) => self.check_lit(cx, lit),
440+
ExprKind::Lit(ref lit) => Self::check_lit(cx, lit),
441441
_ => (),
442442
}
443443
}
@@ -469,7 +469,7 @@ impl EarlyLintPass for MiscEarlyLints {
469469
}
470470

471471
impl MiscEarlyLints {
472-
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
472+
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
473473
// We test if first character in snippet is a number, because the snippet could be an expansion
474474
// from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
475475
// Note that this check also covers special case that `line!()` is eagerly expanded by compiler.

clippy_lints/src/returns.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Return {
117117
ast::ExprKind::Ret(ref inner) => {
118118
// allow `#[cfg(a)] return a; #[cfg(b)] return b;`
119119
if !expr.attrs.iter().any(attr_is_cfg) {
120-
self.emit_return_lint(
120+
Self::emit_return_lint(
121121
cx,
122122
span.expect("`else return` is not possible"),
123123
inner.as_ref().map(|i| i.span),
@@ -146,13 +146,7 @@ impl Return {
146146
}
147147
}
148148

149-
fn emit_return_lint(
150-
&mut self,
151-
cx: &EarlyContext<'_>,
152-
ret_span: Span,
153-
inner_span: Option<Span>,
154-
replacement: RetReplacement,
155-
) {
149+
fn emit_return_lint(cx: &EarlyContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
156150
match inner_span {
157151
Some(inner_span) => {
158152
if in_external_macro(cx.sess(), inner_span) || inner_span.from_expansion() {
@@ -191,7 +185,7 @@ impl Return {
191185
}
192186

193187
// Check for "let x = EXPR; x"
194-
fn check_let_return(&mut self, cx: &EarlyContext<'_>, block: &ast::Block) {
188+
fn check_let_return(cx: &EarlyContext<'_>, block: &ast::Block) {
195189
let mut it = block.stmts.iter();
196190

197191
// we need both a let-binding stmt and an expr
@@ -275,7 +269,7 @@ impl EarlyLintPass for Return {
275269
}
276270

277271
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &ast::Block) {
278-
self.check_let_return(cx, block);
272+
Self::check_let_return(cx, block);
279273
if_chain! {
280274
if let Some(ref stmt) = block.stmts.last();
281275
if let ast::StmtKind::Expr(ref expr) = stmt.kind;

clippy_lints/src/slow_vector_initialization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
244244

245245
// Check that take is applied to `repeat(0)`
246246
if let Some(ref repeat_expr) = take_args.get(0);
247-
if self.is_repeat_zero(repeat_expr);
247+
if Self::is_repeat_zero(repeat_expr);
248248

249249
// Check that len expression is equals to `with_capacity` expression
250250
if let Some(ref len_arg) = take_args.get(1);
@@ -259,7 +259,7 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
259259
}
260260

261261
/// Returns `true` if given expression is `repeat(0)`
262-
fn is_repeat_zero(&self, expr: &Expr) -> bool {
262+
fn is_repeat_zero(expr: &Expr) -> bool {
263263
if_chain! {
264264
if let ExprKind::Call(ref fn_expr, ref repeat_args) = expr.kind;
265265
if let ExprKind::Path(ref qpath_repeat) = fn_expr.kind;

0 commit comments

Comments
 (0)