Skip to content

Commit 6dcc8d5

Browse files
committed
Auto merge of #5141 - xiongmao86:issue5095, r=flip1995
Fixes issue 5095 fixes #5095. - [x] Followed [lint naming conventions][lint_naming] - [x] Added passing UI tests (including committed `.stderr` file) - [x] `cargo test` passes locally - [x] Executed `cargo dev update_lints` - [x] Added lint documentation - [x] Run `cargo dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints changelog: (internal) warn about collapsible `span_lint_and_then` calls.
2 parents 2efc2d6 + 7aeb3a4 commit 6dcc8d5

Some content is hidden

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

52 files changed

+758
-199
lines changed

clippy_lints/src/as_conversions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl EarlyLintPass for AsConversions {
5050
AS_CONVERSIONS,
5151
expr.span,
5252
"using a potentially dangerous silent `as` conversion",
53+
None,
5354
"consider using a safe wrapper for this conversion",
5455
);
5556
}

clippy_lints/src/assertions_on_constants.rs

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
4141
} else {
4242
"`assert!(true)` will be optimized out by the compiler"
4343
},
44+
None,
4445
"remove it",
4546
);
4647
};
@@ -50,6 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
5051
ASSERTIONS_ON_CONSTANTS,
5152
e.span,
5253
"`assert!(false)` should probably be replaced",
54+
None,
5355
"use `panic!()` or `unreachable!()`",
5456
);
5557
};
@@ -59,6 +61,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
5961
ASSERTIONS_ON_CONSTANTS,
6062
e.span,
6163
&format!("`assert!(false, {})` should probably be replaced", panic_message),
64+
None,
6265
&format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
6366
)
6467
};

clippy_lints/src/atomic_ordering.rs

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
8585
INVALID_ATOMIC_ORDERING,
8686
ordering_arg.span,
8787
"atomic loads cannot have `Release` and `AcqRel` ordering",
88+
None,
8889
"consider using ordering modes `Acquire`, `SeqCst` or `Relaxed`"
8990
);
9091
} else if method == "store" &&
@@ -94,6 +95,7 @@ fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
9495
INVALID_ATOMIC_ORDERING,
9596
ordering_arg.span,
9697
"atomic stores cannot have `Acquire` and `AcqRel` ordering",
98+
None,
9799
"consider using ordering modes `Release`, `SeqCst` or `Relaxed`"
98100
);
99101
}
@@ -118,6 +120,7 @@ fn check_memory_fence(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
118120
INVALID_ATOMIC_ORDERING,
119121
args[0].span,
120122
"memory fences cannot have `Relaxed` ordering",
123+
None,
121124
"consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`"
122125
);
123126
}

clippy_lints/src/cognitive_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl CognitiveComplexity {
105105
rust_cc,
106106
self.limit.limit()
107107
),
108+
None,
108109
"you could split it up into multiple smaller functions",
109110
);
110111
}

clippy_lints/src/comparison_chain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ComparisonChain {
104104
COMPARISON_CHAIN,
105105
expr.span,
106106
"`if` chain can be rewritten with `match`",
107+
None,
107108
"Consider rewriting the `if` chain to use `cmp` and `match`.",
108109
)
109110
}

clippy_lints/src/copies.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn lint_same_then_else(cx: &LateContext<'_, '_>, blocks: &[&Block<'_>]) {
183183
IF_SAME_THEN_ELSE,
184184
j.span,
185185
"this `if` has identical blocks",
186-
i.span,
186+
Some(i.span),
187187
"same as this",
188188
);
189189
}
@@ -206,7 +206,7 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
206206
IFS_SAME_COND,
207207
j.span,
208208
"this `if` has the same condition as a previous `if`",
209-
i.span,
209+
Some(i.span),
210210
"same as this",
211211
);
212212
}
@@ -234,7 +234,7 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
234234
SAME_FUNCTIONS_IN_IF_CONDITION,
235235
j.span,
236236
"this `if` has the same function call as a previous `if`",
237-
i.span,
237+
Some(i.span),
238238
"same as this",
239239
);
240240
}

clippy_lints/src/copy_iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
4646
COPY_ITERATOR,
4747
item.span,
4848
"you are implementing `Iterator` on a `Copy` type",
49-
item.span,
49+
None,
5050
"consider implementing `IntoIterator` instead",
5151
);
5252
}

clippy_lints/src/dbg_macro.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl EarlyLintPass for DbgMacro {
4848
DBG_MACRO,
4949
mac.span(),
5050
"`dbg!` macro is intended as a debugging tool",
51+
None,
5152
"ensure to avoid having uses of it in version control",
5253
);
5354
}

clippy_lints/src/derive.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::paths;
2-
use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_then};
2+
use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_note, span_lint_and_then};
33
use if_chain::if_chain;
44
use rustc_hir::{Item, ItemKind, TraitRef};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -163,14 +163,13 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait
163163
_ => (),
164164
}
165165

166-
span_lint_and_then(
166+
span_lint_and_note(
167167
cx,
168168
EXPL_IMPL_CLONE_ON_COPY,
169169
item.span,
170170
"you are implementing `Clone` explicitly on a `Copy` type",
171-
|diag| {
172-
diag.span_note(item.span, "consider deriving `Clone` or removing `Copy`");
173-
},
171+
Some(item.span),
172+
"consider deriving `Clone` or removing `Copy`",
174173
);
175174
}
176175
}

clippy_lints/src/drop_forget_ref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
135135
lint,
136136
expr.span,
137137
&msg,
138-
arg.span,
138+
Some(arg.span),
139139
&format!("argument has type `{}`", arg_ty));
140140
} else if is_copy(cx, arg_ty) {
141141
if match_def_path(cx, def_id, &paths::DROP) {
@@ -151,7 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
151151
lint,
152152
expr.span,
153153
&msg,
154-
arg.span,
154+
Some(arg.span),
155155
&format!("argument has type {}", arg_ty));
156156
}
157157
}

clippy_lints/src/else_if_without_else.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl EarlyLintPass for ElseIfWithoutElse {
6161
ELSE_IF_WITHOUT_ELSE,
6262
els.span,
6363
"`if` expression with an `else if`, but without a final `else`",
64+
None,
6465
"add an `else` block here",
6566
);
6667
}

clippy_lints/src/empty_enum.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint when there is an enum with no variants
22
3-
use crate::utils::span_lint_and_then;
3+
use crate::utils::span_lint_and_help;
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -45,13 +45,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
4545
let ty = cx.tcx.type_of(did);
4646
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
4747
if adt.variants.is_empty() {
48-
span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |diag| {
49-
diag.span_help(
50-
item.span,
51-
"consider using the uninhabited type `!` (never type) or a wrapper \
52-
around it to introduce a type which can't be instantiated",
53-
);
54-
});
48+
span_lint_and_help(
49+
cx,
50+
EMPTY_ENUM,
51+
item.span,
52+
"enum with no variants",
53+
None,
54+
"consider using the uninhabited type `!` (never type) or a wrapper \
55+
around it to introduce a type which can't be instantiated",
56+
);
5557
}
5658
}
5759
}

clippy_lints/src/enum_variants.rs

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ fn check_variant(
206206
lint,
207207
span,
208208
&format!("All variants have the same {}fix: `{}`", what, value),
209+
None,
209210
&format!(
210211
"remove the {}fixes and use full paths to \
211212
the variants instead of glob imports",

clippy_lints/src/eta_reduction.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use rustc_middle::ty::{self, Ty};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88

99
use crate::utils::{
10-
implements_trait, is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then, type_is_unsafe_function,
10+
implements_trait, is_adjusted, iter_input_pats, snippet_opt, span_lint_and_sugg, span_lint_and_then,
11+
type_is_unsafe_function,
1112
};
1213

1314
declare_clippy_lint! {
@@ -131,14 +132,15 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
131132
if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]);
132133

133134
then {
134-
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure found", |diag| {
135-
diag.span_suggestion(
136-
expr.span,
137-
"remove closure as shown",
138-
format!("{}::{}", name, path.ident.name),
139-
Applicability::MachineApplicable,
140-
);
141-
});
135+
span_lint_and_sugg(
136+
cx,
137+
REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
138+
expr.span,
139+
"redundant closure found",
140+
"remove closure as shown",
141+
format!("{}::{}", name, path.ident.name),
142+
Applicability::MachineApplicable,
143+
);
142144
}
143145
);
144146
}

clippy_lints/src/eval_order_dependence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
310310
EVAL_ORDER_DEPENDENCE,
311311
expr.span,
312312
"unsequenced read of a variable",
313-
self.write_expr.span,
313+
Some(self.write_expr.span),
314314
"whether read occurs before this write depends on evaluation order"
315315
);
316316
}

clippy_lints/src/excessive_bools.rs

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl ExcessiveBools {
114114
FN_PARAMS_EXCESSIVE_BOOLS,
115115
span,
116116
&format!("more than {} bools in function parameters", self.max_fn_params_bools),
117+
None,
117118
"consider refactoring bools into two-variant enums",
118119
);
119120
}
@@ -153,6 +154,7 @@ impl EarlyLintPass for ExcessiveBools {
153154
STRUCT_EXCESSIVE_BOOLS,
154155
item.span,
155156
&format!("more than {} bools in a struct", self.max_struct_bools),
157+
None,
156158
"consider using a state machine or refactoring bools into two-variant enums",
157159
);
158160
}

clippy_lints/src/formatting.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
149149
really are doing `.. = ({op} ..)`",
150150
op = op
151151
),
152-
eqop_span,
152+
None,
153153
&format!("to remove this lint, use either `{op}=` or `= {op}`", op = op),
154154
);
155155
}
@@ -188,6 +188,7 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
188188
binop = binop_str,
189189
unop = unop_str
190190
),
191+
None,
191192
&format!(
192193
"put a space between `{binop}` and `{unop}` and remove the space after `{unop}`",
193194
binop = binop_str,
@@ -226,7 +227,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
226227
SUSPICIOUS_ELSE_FORMATTING,
227228
else_span,
228229
&format!("this is an `else {}` but the formatting might hide it", else_desc),
229-
else_span,
230+
None,
230231
&format!(
231232
"to remove this lint, remove the `else` or remove the new line between \
232233
`else` and `{}`",
@@ -265,7 +266,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
265266
POSSIBLE_MISSING_COMMA,
266267
lint_span,
267268
"possibly missing a comma here",
268-
lint_span,
269+
None,
269270
"to remove this lint, add a comma or write the expr in a single line",
270271
);
271272
}
@@ -296,7 +297,7 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
296297
SUSPICIOUS_ELSE_FORMATTING,
297298
else_span,
298299
&format!("this looks like {} but the `else` is missing", looks_like),
299-
else_span,
300+
None,
300301
&format!(
301302
"to remove this lint, add the missing `else` or add a new line before {}",
302303
next_thing,

clippy_lints/src/functions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ fn check_needless_must_use(
431431
DOUBLE_MUST_USE,
432432
fn_header_span,
433433
"this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`",
434+
None,
434435
"either add some descriptive text or remove the attribute",
435436
);
436437
}

clippy_lints/src/identity_conversion.rs

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
2+
match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_sugg,
33
};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
@@ -58,29 +58,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
5858
if same_tys(cx, a, b) {
5959
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
6060

61-
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| {
62-
diag.span_suggestion(
63-
e.span,
64-
"consider removing `.into()`",
65-
sugg,
66-
Applicability::MachineApplicable, // snippet
67-
);
68-
});
61+
span_lint_and_sugg(
62+
cx,
63+
IDENTITY_CONVERSION,
64+
e.span,
65+
"identical conversion",
66+
"consider removing `.into()`",
67+
sugg,
68+
Applicability::MachineApplicable, // snippet
69+
);
6970
}
7071
}
7172
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
7273
let a = cx.tables.expr_ty(e);
7374
let b = cx.tables.expr_ty(&args[0]);
7475
if same_tys(cx, a, b) {
7576
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
76-
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| {
77-
diag.span_suggestion(
78-
e.span,
79-
"consider removing `.into_iter()`",
80-
sugg,
81-
Applicability::MachineApplicable, // snippet
82-
);
83-
});
77+
span_lint_and_sugg(
78+
cx,
79+
IDENTITY_CONVERSION,
80+
e.span,
81+
"identical conversion",
82+
"consider removing `.into_iter()`",
83+
sugg,
84+
Applicability::MachineApplicable, // snippet
85+
);
8486
}
8587
}
8688
},
@@ -95,14 +97,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
9597
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
9698
let sugg_msg =
9799
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
98-
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| {
99-
diag.span_suggestion(
100-
e.span,
101-
&sugg_msg,
102-
sugg,
103-
Applicability::MachineApplicable, // snippet
104-
);
105-
});
100+
span_lint_and_sugg(
101+
cx,
102+
IDENTITY_CONVERSION,
103+
e.span,
104+
"identical conversion",
105+
&sugg_msg,
106+
sugg,
107+
Applicability::MachineApplicable, // snippet
108+
);
106109
}
107110
}
108111
}

0 commit comments

Comments
 (0)