Skip to content

Commit 06ec3d3

Browse files
authored
Merge pull request #1841 from sanxiyn/span-lint-and-sugg
Use span_lint_and_sugg
2 parents 88101d5 + 745233f commit 06ec3d3

12 files changed

+91
-122
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use rustc::lint::*;
1616
use syntax::ast;
1717

18-
use utils::{in_macro, snippet_block, span_lint_and_then};
18+
use utils::{in_macro, snippet_block, span_lint_and_then, span_lint_and_sugg};
1919
use utils::sugg::Sugg;
2020

2121
/// **What it does:** Checks for nested `if` statements which can be collapsed
@@ -108,12 +108,12 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext, else_: &ast::Expr) {
108108
], {
109109
match else_.node {
110110
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
111-
span_lint_and_then(cx,
111+
span_lint_and_sugg(cx,
112112
COLLAPSIBLE_IF,
113113
block.span,
114-
"this `else { if .. }` block can be collapsed", |db| {
115-
db.span_suggestion(block.span, "try", snippet_block(cx, else_.span, "..").into_owned());
116-
});
114+
"this `else { if .. }` block can be collapsed",
115+
"try",
116+
snippet_block(cx, else_.span, "..").into_owned());
117117
}
118118
_ => (),
119119
}

clippy_lints/src/len_zero.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::ty;
44
use rustc::hir::*;
55
use syntax::ast::{Lit, LitKind, Name};
66
use syntax::codemap::{Span, Spanned};
7-
use utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_then, walk_ptrs_ty};
7+
use utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_sugg, walk_ptrs_ty};
88

99
/// **What it does:** Checks for getting the length of something via `.len()`
1010
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
@@ -171,11 +171,9 @@ fn check_cmp(cx: &LateContext, span: Span, left: &Expr, right: &Expr, op: &str)
171171
fn check_len_zero(cx: &LateContext, span: Span, name: Name, args: &[Expr], lit: &Lit, op: &str) {
172172
if let Spanned { node: LitKind::Int(0, _), .. } = *lit {
173173
if name == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
174-
span_lint_and_then(cx, LEN_ZERO, span, "length comparison to zero", |db| {
175-
db.span_suggestion(span,
176-
"using `is_empty` is more concise:",
177-
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")));
178-
});
174+
span_lint_and_sugg(cx, LEN_ZERO, span, "length comparison to zero",
175+
"using `is_empty` is more concise:",
176+
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")));
179177
}
180178
}
181179
}

clippy_lints/src/loops.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
377377
// 1) it was ugly with big bodies;
378378
// 2) it was not indented properly;
379379
// 3) it wasn’t very smart (see #675).
380-
span_lint_and_then(cx,
380+
span_lint_and_sugg(cx,
381381
WHILE_LET_LOOP,
382382
expr.span,
383383
"this loop could be written as a `while let` loop",
384-
|db| {
385-
let sug = format!("while let {} = {} {{ .. }}",
386-
snippet(cx, arms[0].pats[0].span, ".."),
387-
snippet(cx, matchexpr.span, ".."));
388-
db.span_suggestion(expr.span, "try", sug);
389-
});
384+
"try",
385+
format!("while let {} = {} {{ .. }}",
386+
snippet(cx, arms[0].pats[0].span, ".."),
387+
snippet(cx, matchexpr.span, "..")));
390388
}
391389
},
392390
_ => (),
@@ -405,13 +403,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
405403
!is_iterator_used_after_while_let(cx, iter_expr) {
406404
let iterator = snippet(cx, method_args[0].span, "_");
407405
let loop_var = snippet(cx, pat_args[0].span, "_");
408-
span_lint_and_then(cx,
406+
span_lint_and_sugg(cx,
409407
WHILE_LET_ON_ITERATOR,
410408
expr.span,
411409
"this loop could be written as a `for` loop",
412-
|db| {
413-
db.span_suggestion(expr.span, "try", format!("for {} in {} {{ .. }}", loop_var, iterator));
414-
});
410+
"try",
411+
format!("for {} in {} {{ .. }}", loop_var, iterator));
415412
}
416413
}
417414
}

clippy_lints/src/matches.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::collections::Bound;
99
use syntax::ast::LitKind;
1010
use syntax::codemap::Span;
1111
use utils::paths;
12-
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty,
12+
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, span_lint_and_sugg, in_external_macro, expr_block, walk_ptrs_ty,
1313
is_expn_of, remove_blocks};
1414
use utils::sugg::Sugg;
1515

@@ -210,20 +210,17 @@ fn report_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm],
210210
SINGLE_MATCH
211211
};
212212
let els_str = els.map_or(String::new(), |els| format!(" else {}", expr_block(cx, els, None, "..")));
213-
span_lint_and_then(cx,
213+
span_lint_and_sugg(cx,
214214
lint,
215215
expr.span,
216216
"you seem to be trying to use match for destructuring a single pattern. Consider using `if \
217217
let`",
218-
|db| {
219-
db.span_suggestion(expr.span,
220-
"try this",
221-
format!("if let {} = {} {}{}",
222-
snippet(cx, arms[0].pats[0].span, ".."),
223-
snippet(cx, ex.span, ".."),
224-
expr_block(cx, &arms[0].body, None, ".."),
225-
els_str));
226-
});
218+
"try this",
219+
format!("if let {} = {} {}{}",
220+
snippet(cx, arms[0].pats[0].span, ".."),
221+
snippet(cx, ex.span, ".."),
222+
expr_block(cx, &arms[0].body, None, ".."),
223+
els_str));
227224
}
228225

229226
fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr, ty: Ty, els: Option<&Expr>) {

clippy_lints/src/methods.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::borrow::Cow;
88
use std::fmt;
99
use syntax::codemap::Span;
1010
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, match_path, match_trait_method,
11-
match_type, method_chain_args, return_ty, same_tys, snippet, span_lint, span_lint_and_then,
11+
match_type, method_chain_args, return_ty, same_tys, snippet, span_lint, span_lint_and_then, span_lint_and_sugg,
1212
span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, last_path_segment, single_segment_path,
1313
match_def_path, is_self, is_self_ty, iter_input_pats, match_path_old};
1414
use utils::paths;
@@ -725,15 +725,12 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
725725
};
726726

727727
if implements_trait(cx, arg_ty, default_trait_id, &[]) {
728-
span_lint_and_then(cx,
728+
span_lint_and_sugg(cx,
729729
OR_FUN_CALL,
730730
span,
731731
&format!("use of `{}` followed by a call to `{}`", name, path),
732-
|db| {
733-
db.span_suggestion(span,
734-
"try this",
735-
format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")));
736-
});
732+
"try this",
733+
format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")));
737734
return true;
738735
}
739736
}
@@ -791,15 +788,12 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
791788
(false, true) => snippet(cx, fun_span, ".."),
792789
};
793790

794-
span_lint_and_then(cx,
791+
span_lint_and_sugg(cx,
795792
OR_FUN_CALL,
796793
span,
797794
&format!("use of `{}` followed by a function call", name),
798-
|db| {
799-
db.span_suggestion(span,
800-
"try this",
801-
format!("{}.{}_{}({})", snippet(cx, self_expr.span, "_"), name, suffix, sugg));
802-
});
795+
"try this",
796+
format!("{}.{}_{}({})", snippet(cx, self_expr.span, "_"), name, suffix, sugg));
803797
}
804798

805799
if args.len() == 2 {
@@ -865,14 +859,12 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) {
865859
return;
866860
};
867861

868-
span_lint_and_then(cx, STRING_EXTEND_CHARS, expr.span, "calling `.extend(_.chars())`", |db| {
869-
db.span_suggestion(expr.span,
870-
"try this",
871-
format!("{}.push_str({}{})",
872-
snippet(cx, args[0].span, "_"),
873-
ref_str,
874-
snippet(cx, target.span, "_")));
875-
});
862+
span_lint_and_sugg(cx, STRING_EXTEND_CHARS, expr.span, "calling `.extend(_.chars())`",
863+
"try this",
864+
format!("{}.push_str({}{})",
865+
snippet(cx, args[0].span, "_"),
866+
ref_str,
867+
snippet(cx, target.span, "_")));
876868
}
877869
}
878870

@@ -951,20 +943,17 @@ fn lint_get_unwrap(cx: &LateContext, expr: &hir::Expr, get_args: &[hir::Expr], i
951943

952944
let mut_str = if is_mut { "_mut" } else { "" };
953945
let borrow_str = if is_mut { "&mut " } else { "&" };
954-
span_lint_and_then(cx,
946+
span_lint_and_sugg(cx,
955947
GET_UNWRAP,
956948
expr.span,
957949
&format!("called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
958950
mut_str,
959951
caller_type),
960-
|db| {
961-
db.span_suggestion(expr.span,
962-
"try this",
963-
format!("{}{}[{}]",
964-
borrow_str,
965-
snippet(cx, get_args[0].span, "_"),
966-
snippet(cx, get_args[1].span, "_")));
967-
});
952+
"try this",
953+
format!("{}{}[{}]",
954+
borrow_str,
955+
snippet(cx, get_args[0].span, "_"),
956+
snippet(cx, get_args[1].span, "_")));
968957
}
969958

970959
fn lint_iter_skip_next(cx: &LateContext, expr: &hir::Expr) {
@@ -1216,19 +1205,15 @@ fn lint_chars_next(cx: &LateContext, expr: &hir::Expr, chain: &hir::Expr, other:
12161205
return false;
12171206
}
12181207

1219-
span_lint_and_then(cx,
1208+
span_lint_and_sugg(cx,
12201209
CHARS_NEXT_CMP,
12211210
expr.span,
12221211
"you should use the `starts_with` method",
1223-
|db| {
1224-
let sugg = format!("{}{}.starts_with({})",
1225-
if eq { "" } else { "!" },
1226-
snippet(cx, args[0][0].span, "_"),
1227-
snippet(cx, arg_char[0].span, "_")
1228-
);
1229-
1230-
db.span_suggestion(expr.span, "like this", sugg);
1231-
});
1212+
"like this",
1213+
format!("{}{}.starts_with({})",
1214+
if eq { "" } else { "!" },
1215+
snippet(cx, args[0][0].span, "_"),
1216+
snippet(cx, arg_char[0].span, "_")));
12321217

12331218
return true;
12341219
}}

clippy_lints/src/needless_bool.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::lint::*;
66
use rustc::hir::*;
77
use syntax::ast::LitKind;
88
use syntax::codemap::Spanned;
9-
use utils::{span_lint, span_lint_and_then, snippet};
9+
use utils::{span_lint, span_lint_and_sugg, snippet};
1010
use utils::sugg::Sugg;
1111

1212
/// **What it does:** Checks for expressions of the form `if c { true } else { false }`
@@ -70,11 +70,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
7070
snip.to_string()
7171
};
7272

73-
span_lint_and_then(cx,
73+
span_lint_and_sugg(cx,
7474
NEEDLESS_BOOL,
7575
e.span,
7676
"this if-then-else expression returns a bool literal",
77-
|db| { db.span_suggestion(e.span, "you can reduce it to", hint); });
77+
"you can reduce it to",
78+
hint);
7879
};
7980
if let ExprBlock(ref then_block) = then_block.node {
8081
match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
@@ -121,39 +122,39 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
121122
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
122123
(Bool(true), Other) => {
123124
let hint = snippet(cx, right_side.span, "..").into_owned();
124-
span_lint_and_then(cx,
125+
span_lint_and_sugg(cx,
125126
BOOL_COMPARISON,
126127
e.span,
127128
"equality checks against true are unnecessary",
128-
|db| { db.span_suggestion(e.span, "try simplifying it as shown:", hint); });
129+
"try simplifying it as shown:",
130+
hint);
129131
},
130132
(Other, Bool(true)) => {
131133
let hint = snippet(cx, left_side.span, "..").into_owned();
132-
span_lint_and_then(cx,
134+
span_lint_and_sugg(cx,
133135
BOOL_COMPARISON,
134136
e.span,
135137
"equality checks against true are unnecessary",
136-
|db| { db.span_suggestion(e.span, "try simplifying it as shown:", hint); });
138+
"try simplifying it as shown:",
139+
hint);
137140
},
138141
(Bool(false), Other) => {
139142
let hint = Sugg::hir(cx, right_side, "..");
140-
span_lint_and_then(cx,
143+
span_lint_and_sugg(cx,
141144
BOOL_COMPARISON,
142145
e.span,
143146
"equality checks against false can be replaced by a negation",
144-
|db| {
145-
db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
146-
});
147+
"try simplifying it as shown:",
148+
(!hint).to_string());
147149
},
148150
(Other, Bool(false)) => {
149151
let hint = Sugg::hir(cx, left_side, "..");
150-
span_lint_and_then(cx,
152+
span_lint_and_sugg(cx,
151153
BOOL_COMPARISON,
152154
e.span,
153155
"equality checks against false can be replaced by a negation",
154-
|db| {
155-
db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
156-
});
156+
"try simplifying it as shown:",
157+
(!hint).to_string());
157158
},
158159
_ => (),
159160
}

clippy_lints/src/no_effect.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
22
use rustc::hir::def::Def;
33
use rustc::hir::{Expr, Expr_, Stmt, StmtSemi, BlockCheckMode, UnsafeSource, BiAnd, BiOr};
4-
use utils::{in_macro, span_lint, snippet_opt, span_lint_and_then};
4+
use utils::{in_macro, span_lint, snippet_opt, span_lint_and_sugg};
55
use std::ops::Deref;
66

77
/// **What it does:** Checks for statements which have no effect.
@@ -120,11 +120,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
120120
return;
121121
}
122122
}
123-
span_lint_and_then(cx,
123+
span_lint_and_sugg(cx,
124124
UNNECESSARY_OPERATION,
125125
stmt.span,
126126
"statement can be reduced",
127-
|db| { db.span_suggestion(stmt.span, "replace it with", snippet); });
127+
"replace it with",
128+
snippet);
128129
}
129130
}
130131
}

clippy_lints/src/precedence.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::lint::*;
22
use syntax::ast::*;
33
use syntax::codemap::Spanned;
4-
use utils::{span_lint_and_then, snippet};
4+
use utils::{span_lint_and_sugg, snippet};
55

66
/// **What it does:** Checks for operations where precedence may be unclear
77
/// and suggests to add parentheses. Currently it catches the following:
@@ -38,9 +38,8 @@ impl EarlyLintPass for Precedence {
3838
if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node {
3939
let span_sugg =
4040
|expr: &Expr, sugg| {
41-
span_lint_and_then(cx, PRECEDENCE, expr.span, "operator precedence can trip the unwary", |db| {
42-
db.span_suggestion(expr.span, "consider parenthesizing your expression", sugg);
43-
});
41+
span_lint_and_sugg(cx, PRECEDENCE, expr.span, "operator precedence can trip the unwary",
42+
"consider parenthesizing your expression", sugg);
4443
};
4544

4645
if !is_bit_op(op) {
@@ -80,15 +79,12 @@ impl EarlyLintPass for Precedence {
8079
LitKind::Int(..) |
8180
LitKind::Float(..) |
8281
LitKind::FloatUnsuffixed(..) => {
83-
span_lint_and_then(cx,
82+
span_lint_and_sugg(cx,
8483
PRECEDENCE,
8584
expr.span,
8685
"unary minus has lower precedence than method call",
87-
|db| {
88-
db.span_suggestion(expr.span,
89-
"consider adding parentheses to clarify your intent",
90-
format!("-({})", snippet(cx, rhs.span, "..")));
91-
});
86+
"consider adding parentheses to clarify your intent",
87+
format!("-({})", snippet(cx, rhs.span, "..")));
9288
},
9389
_ => (),
9490
}

0 commit comments

Comments
 (0)