Skip to content

Commit aceab00

Browse files
committed
Auto merge of #4575 - Manishearth:suggestions, r=oli-obk
Make more tests rustfixable Burning through #3630 changelog: Improve suggestions for many lints in preparation for `cargo fix --clippy` r? @phansch @yaahc
2 parents d5ec41c + 472745c commit aceab00

File tree

103 files changed

+1718
-964
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1718
-964
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util"}
4646

4747
[dev-dependencies]
4848
cargo_metadata = "0.8.0"
49-
compiletest_rs = { version = "0.3.22", features = ["tmp"] }
49+
compiletest_rs = { version = "0.3.23", features = ["tmp"] }
5050
lazy_static = "1.0"
5151
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
5252
serde = { version = "1.0", features = ["derive"] }

clippy_lints/src/cognitive_complexity.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl<'tcx> Visitor<'tcx> for CCHelper {
112112
walk_expr(self, e);
113113
match e.node {
114114
ExprKind::Match(_, ref arms, _) => {
115-
let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum();
116-
if arms_n > 1 {
115+
if arms.len() > 1 {
117116
self.cc += 1;
118117
}
119118
self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;

clippy_lints/src/copies.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
193193
(min_index..=max_index).all(|index| arms[index].guard.is_none()) &&
194194
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
195195
// all patterns should have the same bindings
196-
same_bindings(cx, &bindings(cx, &lhs.pats[0]), &bindings(cx, &rhs.pats[0]))
196+
same_bindings(cx, &bindings(cx, &lhs.pat), &bindings(cx, &rhs.pat))
197197
};
198198

199199
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();
@@ -213,27 +213,22 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
213213
// span for the whole pattern, the suggestion is only shown when there is only
214214
// one pattern. The user should know about `|` if they are already using it…
215215

216-
if i.pats.len() == 1 && j.pats.len() == 1 {
217-
let lhs = snippet(cx, i.pats[0].span, "<pat1>");
218-
let rhs = snippet(cx, j.pats[0].span, "<pat2>");
219-
220-
if let PatKind::Wild = j.pats[0].node {
221-
// if the last arm is _, then i could be integrated into _
222-
// note that i.pats[0] cannot be _, because that would mean that we're
223-
// hiding all the subsequent arms, and rust won't compile
224-
db.span_note(
225-
i.body.span,
226-
&format!(
227-
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
228-
lhs
229-
),
230-
);
231-
} else {
232-
db.span_help(
233-
i.pats[0].span,
234-
&format!("consider refactoring into `{} | {}`", lhs, rhs),
235-
);
236-
}
216+
let lhs = snippet(cx, i.pat.span, "<pat1>");
217+
let rhs = snippet(cx, j.pat.span, "<pat2>");
218+
219+
if let PatKind::Wild = j.pat.node {
220+
// if the last arm is _, then i could be integrated into _
221+
// note that i.pat cannot be _, because that would mean that we're
222+
// hiding all the subsequent arms, and rust won't compile
223+
db.span_note(
224+
i.body.span,
225+
&format!(
226+
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
227+
lhs
228+
),
229+
);
230+
} else {
231+
db.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
237232
}
238233
},
239234
);

clippy_lints/src/entry.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::SpanlessEq;
2-
use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty};
2+
use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt};
3+
use crate::utils::{snippet_with_applicability, span_lint_and_then, walk_ptrs_ty};
34
use if_chain::if_chain;
45
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
56
use rustc::hir::*;
@@ -64,6 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
6465
} else {
6566
true
6667
}
68+
// XXXManishearth we can also check for if/else blocks containing `None`.
6769
};
6870

6971
let mut visitor = InsertVisitor {
@@ -145,10 +147,11 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
145147
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
146148
&format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {
147149
if self.sole_expr {
148-
let help = format!("{}.entry({}).or_insert({})",
149-
snippet(self.cx, self.map.span, "map"),
150-
snippet(self.cx, params[1].span, ".."),
151-
snippet(self.cx, params[2].span, ".."));
150+
let mut app = Applicability::MachineApplicable;
151+
let help = format!("{}.entry({}).or_insert({});",
152+
snippet_with_applicability(self.cx, self.map.span, "map", &mut app),
153+
snippet_with_applicability(self.cx, params[1].span, "..", &mut app),
154+
snippet_with_applicability(self.cx, params[2].span, "..", &mut app));
152155

153156
db.span_suggestion(
154157
self.span,
@@ -158,15 +161,13 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
158161
);
159162
}
160163
else {
161-
let help = format!("{}.entry({})",
164+
let help = format!("consider using `{}.entry({})`",
162165
snippet(self.cx, self.map.span, "map"),
163166
snippet(self.cx, params[1].span, ".."));
164167

165-
db.span_suggestion(
168+
db.span_label(
166169
self.span,
167-
"consider using",
168-
help,
169-
Applicability::MachineApplicable, // snippet
170+
&help,
170171
);
171172
}
172173
});

clippy_lints/src/format.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
8484
if let ExprKind::Path(ref qpath) = args[1].node;
8585
if let Some(did) = resolve_node(cx, qpath, args[1].hir_id).opt_def_id();
8686
if match_def_path(cx, did, &paths::DISPLAY_FMT_METHOD);
87-
if arms[0].pats.len() == 1;
8887
// check `(arg0,)` in match block
89-
if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
88+
if let PatKind::Tuple(ref pats, None) = arms[0].pat.node;
9089
if pats.len() == 1;
9190
then {
9291
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));

clippy_lints/src/infallible_destructuring_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
4747
if_chain! {
4848
if let Some(ref expr) = local.init;
4949
if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
50-
if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
51-
if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
50+
if arms.len() == 1 && arms[0].guard.is_none();
51+
if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.node;
5252
if args.len() == 1;
5353
if let Some(arg) = get_arg_name(&args[0]);
5454
let body = remove_blocks(&arms[0].body);

clippy_lints/src/loops.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
517517
match *source {
518518
MatchSource::Normal | MatchSource::IfLetDesugar { .. } => {
519519
if arms.len() == 2
520-
&& arms[0].pats.len() == 1
521520
&& arms[0].guard.is_none()
522-
&& arms[1].pats.len() == 1
523521
&& arms[1].guard.is_none()
524522
&& is_simple_break_expr(&arms[1].body)
525523
{
@@ -541,7 +539,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
541539
"try",
542540
format!(
543541
"while let {} = {} {{ .. }}",
544-
snippet_with_applicability(cx, arms[0].pats[0].span, "..", &mut applicability),
542+
snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability),
545543
snippet_with_applicability(cx, matchexpr.span, "..", &mut applicability),
546544
),
547545
applicability,
@@ -554,7 +552,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
554552
}
555553
}
556554
if let ExprKind::Match(ref match_expr, ref arms, MatchSource::WhileLetDesugar) = expr.node {
557-
let pat = &arms[0].pats[0].node;
555+
let pat = &arms[0].pat.node;
558556
if let (
559557
&PatKind::TupleStruct(ref qpath, ref pat_args, _),
560558
&ExprKind::MethodCall(ref method_path, _, ref method_args),
@@ -2429,12 +2427,17 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>
24292427
let contains_arg = snippet(cx, args[1].span, "??");
24302428
let span = shorten_needless_collect_span(expr);
24312429
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2430+
let (arg, pred) = if contains_arg.starts_with('&') {
2431+
("x", &contains_arg[1..])
2432+
} else {
2433+
("&x", &*contains_arg)
2434+
};
24322435
db.span_suggestion(
24332436
span,
24342437
"replace with",
24352438
format!(
2436-
".any(|&x| x == {})",
2437-
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
2439+
".any(|{}| x == {})",
2440+
arg, pred
24382441
),
24392442
Applicability::MachineApplicable,
24402443
);

clippy_lints/src/map_unit_fn.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr
217217
if is_unit_function(cx, fn_arg) {
218218
let msg = suggestion_msg("function", map_type);
219219
let suggestion = format!(
220-
"if let {0}({1}) = {2} {{ {3}(...) }}",
220+
"if let {0}({binding}) = {1} {{ {2}({binding}) }}",
221221
variant,
222-
let_binding_name(cx, var_arg),
223222
snippet(cx, var_arg.span, "_"),
224-
snippet(cx, fn_arg.span, "_")
223+
snippet(cx, fn_arg.span, "_"),
224+
binding = let_binding_name(cx, var_arg)
225225
);
226226

227227
span_lint_and_then(cx, lint, expr.span, &msg, |db| {
228-
db.span_suggestion(stmt.span, "try this", suggestion, Applicability::Unspecified);
228+
db.span_suggestion(stmt.span, "try this", suggestion, Applicability::MachineApplicable);
229229
});
230230
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
231231
let msg = suggestion_msg("closure", map_type);
@@ -250,9 +250,9 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr
250250
"if let {0}({1}) = {2} {{ ... }}",
251251
variant,
252252
snippet(cx, binding.pat.span, "_"),
253-
snippet(cx, var_arg.span, "_")
253+
snippet(cx, var_arg.span, "_"),
254254
);
255-
db.span_suggestion(stmt.span, "try this", suggestion, Applicability::Unspecified);
255+
db.span_suggestion(stmt.span, "try this", suggestion, Applicability::HasPlaceholders);
256256
}
257257
});
258258
}

0 commit comments

Comments
 (0)