Skip to content

Commit 4e74eef

Browse files
committed
Add applicability level to (nearly) every span_lint_and_sugg function
1 parent 0c6483b commit 4e74eef

29 files changed

+216
-148
lines changed

clippy_lints/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl EarlyLintPass for CfgAttrPass {
532532
"`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
533533
"use",
534534
format!("{}rustfmt::skip]", attr_style),
535-
Applicability::Unspecified,
535+
Applicability::MachineApplicable,
536536
);
537537
}
538538
}

clippy_lints/src/bytecount.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::rustc::{declare_tool_lint, lint_array};
1515
use crate::rustc_errors::Applicability;
1616
use crate::syntax::ast::{Name, UintTy};
1717
use crate::utils::{
18-
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet, span_lint_and_sugg, walk_ptrs_ty,
18+
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability,
19+
span_lint_and_sugg, walk_ptrs_ty,
1920
};
2021
use if_chain::if_chain;
2122

@@ -91,15 +92,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
9192
} else {
9293
&filter_args[0]
9394
};
95+
let mut applicability = Applicability::MachineApplicable;
9496
span_lint_and_sugg(
9597
cx,
9698
NAIVE_BYTECOUNT,
9799
expr.span,
98100
"You appear to be counting bytes the naive way",
99101
"Consider using the bytecount crate",
100102
format!("bytecount::count({}, {})",
101-
snippet(cx, haystack.span, ".."),
102-
snippet(cx, needle.span, "..")),
103+
snippet_with_applicability(cx, haystack.span, "..", &mut applicability),
104+
snippet_with_applicability(cx, needle.span, "..", &mut applicability)),
103105
Applicability::Unspecified,
104106
);
105107
}

clippy_lints/src/collapsible_if.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::rustc::{declare_tool_lint, lint_array};
2727
use if_chain::if_chain;
2828
use crate::syntax::ast;
2929

30-
use crate::utils::{in_macro, snippet_block, span_lint_and_sugg, span_lint_and_then};
30+
use crate::utils::{in_macro, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then};
3131
use crate::utils::sugg::Sugg;
3232
use crate::rustc_errors::Applicability;
3333

@@ -128,14 +128,15 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
128128
then {
129129
match else_.node {
130130
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
131+
let mut applicability = Applicability::MachineApplicable;
131132
span_lint_and_sugg(
132133
cx,
133134
COLLAPSIBLE_IF,
134135
block.span,
135136
"this `else { if .. }` block can be collapsed",
136137
"try",
137-
snippet_block(cx, else_.span, "..").into_owned(),
138-
Applicability::Unspecified,
138+
snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(),
139+
applicability,
139140
);
140141
}
141142
_ => (),

clippy_lints/src/default_trait_access.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
8282
&format!("Calling {} is more clear than this expression", replacement),
8383
"try",
8484
replacement,
85-
Applicability::Unspecified,
85+
Applicability::Unspecified, // First resolve the TODO above
8686
);
8787
}
8888
},

clippy_lints/src/double_comparison.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::rustc::{declare_tool_lint, lint_array};
1616
use crate::rustc_errors::Applicability;
1717
use crate::syntax::source_map::Span;
1818

19-
use crate::utils::{snippet, span_lint_and_sugg, SpanlessEq};
19+
use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
2020

2121
/// **What it does:** Checks for double comparions that could be simpified to a single expression.
2222
///
@@ -71,8 +71,9 @@ impl<'a, 'tcx> Pass {
7171
}
7272
macro_rules! lint_double_comparison {
7373
($op:tt) => {{
74-
let lhs_str = snippet(cx, llhs.span, "");
75-
let rhs_str = snippet(cx, lrhs.span, "");
74+
let mut applicability = Applicability::MachineApplicable;
75+
let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
76+
let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
7677
let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
7778
span_lint_and_sugg(
7879
cx,
@@ -81,7 +82,7 @@ impl<'a, 'tcx> Pass {
8182
"This binary expression can be simplified",
8283
"try",
8384
sugg,
84-
Applicability::Unspecified,
85+
applicability,
8586
);
8687
}}
8788
}

clippy_lints/src/duration_subsec.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use if_chain::if_chain;
1717

1818
use crate::consts::{constant, Constant};
1919
use crate::utils::paths;
20-
use crate::utils::{match_type, snippet, span_lint_and_sugg, walk_ptrs_ty};
20+
use crate::utils::{match_type, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty};
2121

2222
/// **What it does:** Checks for calculation of subsecond microseconds or milliseconds
2323
/// from other `Duration` methods.
@@ -61,14 +61,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
6161
("subsec_nanos", 1_000) => "subsec_micros",
6262
_ => return,
6363
};
64+
let mut applicability = Applicability::MachineApplicable;
6465
span_lint_and_sugg(
6566
cx,
6667
DURATION_SUBSEC,
6768
expr.span,
6869
&format!("Calling `{}()` is more concise than this calculation", suggested_fn),
6970
"try",
70-
format!("{}.{}()", snippet(cx, args[0].span, "_"), suggested_fn),
71-
Applicability::Unspecified,
71+
format!("{}.{}()", snippet_with_applicability(cx, args[0].span, "_", &mut applicability), suggested_fn),
72+
applicability,
7273
);
7374
}
7475
}

clippy_lints/src/excessive_precision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
6969
"float has excessive precision",
7070
"consider changing the type or truncating it to",
7171
sugg,
72-
Applicability::Unspecified,
72+
Applicability::MachineApplicable,
7373
);
7474
}
7575
}

clippy_lints/src/infallible_destructuring_match.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to those terms.
99

1010

11-
use super::utils::{get_arg_name, match_var, remove_blocks, snippet, span_lint_and_sugg};
11+
use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
1212
use crate::rustc::hir::*;
1313
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1414
use crate::rustc::{declare_tool_lint, lint_array};
@@ -72,6 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7272
if match_var(body, arg);
7373

7474
then {
75+
let mut applicability = Applicability::MachineApplicable;
7576
span_lint_and_sugg(
7677
cx,
7778
INFALLIBLE_DESTRUCTURING_MATCH,
@@ -81,11 +82,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8182
"try this",
8283
format!(
8384
"let {}({}) = {};",
84-
snippet(cx, variant_name.span, ".."),
85-
snippet(cx, local.pat.span, ".."),
86-
snippet(cx, target.span, ".."),
85+
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
86+
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
87+
snippet_with_applicability(cx, target.span, "..", &mut applicability),
8788
),
88-
Applicability::Unspecified,
89+
applicability,
8990
);
9091
}
9192
}

clippy_lints/src/len_zero.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::rustc_data_structures::fx::FxHashSet;
1717
use crate::rustc_errors::Applicability;
1818
use crate::syntax::ast::{Lit, LitKind, Name};
1919
use crate::syntax::source_map::{Span, Spanned};
20-
use crate::utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_sugg, walk_ptrs_ty};
20+
use crate::utils::{get_item_name, in_macro, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
2121

2222
/// **What it does:** Checks for getting the length of something via `.len()`
2323
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
@@ -224,7 +224,15 @@ fn check_cmp(cx: &LateContext<'_, '_>, span: Span, method: &Expr, lit: &Expr, op
224224
}
225225
}
226226

227-
fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Expr], lit: &Lit, op: &str, compare_to: u32) {
227+
fn check_len(
228+
cx: &LateContext<'_, '_>,
229+
span: Span,
230+
method_name: Name,
231+
args: &[Expr],
232+
lit: &Lit,
233+
op: &str,
234+
compare_to: u32,
235+
) {
228236
if let Spanned {
229237
node: LitKind::Int(lit, _),
230238
..
@@ -236,14 +244,15 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex
236244
}
237245

238246
if method_name == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
247+
let mut applicability = Applicability::MachineApplicable;
239248
span_lint_and_sugg(
240249
cx,
241250
LEN_ZERO,
242251
span,
243252
&format!("length comparison to {}", if compare_to == 0 { "zero" } else { "one" }),
244253
"using `is_empty` is clearer and more explicit",
245-
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")),
246-
Applicability::Unspecified,
254+
format!("{}{}.is_empty()", op, snippet_with_applicability(cx, args[0].span, "_", &mut applicability)),
255+
applicability,
247256
);
248257
}
249258
}

clippy_lints/src/literal_representation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl WarningType {
301301
"mistyped literal suffix",
302302
"did you mean to write",
303303
grouping_hint.to_string(),
304-
Applicability::Unspecified,
304+
Applicability::MachineApplicable,
305305
),
306306
WarningType::UnreadableLiteral => span_lint_and_sugg(
307307
cx,
@@ -310,7 +310,7 @@ impl WarningType {
310310
"long literal lacking separators",
311311
"consider",
312312
grouping_hint.to_owned(),
313-
Applicability::Unspecified,
313+
Applicability::MachineApplicable,
314314
),
315315
WarningType::LargeDigitGroups => span_lint_and_sugg(
316316
cx,
@@ -319,7 +319,7 @@ impl WarningType {
319319
"digit groups should be smaller",
320320
"consider",
321321
grouping_hint.to_owned(),
322-
Applicability::Unspecified,
322+
Applicability::MachineApplicable,
323323
),
324324
WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
325325
cx,
@@ -328,7 +328,7 @@ impl WarningType {
328328
"digits grouped inconsistently by underscores",
329329
"consider",
330330
grouping_hint.to_owned(),
331-
Applicability::Unspecified,
331+
Applicability::MachineApplicable,
332332
),
333333
WarningType::DecimalRepresentation => span_lint_and_sugg(
334334
cx,
@@ -337,7 +337,7 @@ impl WarningType {
337337
"integer literal has a better hexadecimal representation",
338338
"consider",
339339
grouping_hint.to_owned(),
340-
Applicability::Unspecified,
340+
Applicability::MachineApplicable,
341341
),
342342
};
343343
}

clippy_lints/src/loops.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ use crate::utils::{in_macro, sugg, sext};
3535
use crate::utils::usage::mutated_variables;
3636
use crate::consts::{constant, Constant};
3737

38-
use crate::utils::{get_enclosing_block, get_parent_expr, higher, is_integer_literal, is_refutable,
39-
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt,
40-
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq};
4138
use crate::utils::paths;
39+
use crate::utils::{
40+
get_enclosing_block, get_parent_expr, higher, is_integer_literal, is_refutable, last_path_segment,
41+
match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt, snippet_with_applicability,
42+
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq,
43+
};
4244

4345
/// **What it does:** Checks for for-loops that manually copy items between
4446
/// slices that could be optimized by having a memcpy.
@@ -501,6 +503,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
501503
// 1) it was ugly with big bodies;
502504
// 2) it was not indented properly;
503505
// 3) it wasn’t very smart (see #675).
506+
let mut applicability = Applicability::MachineApplicable;
504507
span_lint_and_sugg(
505508
cx,
506509
WHILE_LET_LOOP,
@@ -509,10 +512,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
509512
"try",
510513
format!(
511514
"while let {} = {} {{ .. }}",
512-
snippet(cx, arms[0].pats[0].span, ".."),
513-
snippet(cx, matchexpr.span, "..")
515+
snippet_with_applicability(cx, arms[0].pats[0].span, "..", &mut applicability),
516+
snippet_with_applicability(cx, matchexpr.span, "..", &mut applicability),
514517
),
515-
Applicability::Unspecified,
518+
applicability,
516519
);
517520
}
518521
},
@@ -550,7 +553,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
550553
"this loop could be written as a `for` loop",
551554
"try",
552555
format!("for {} in {} {{ .. }}", loop_var, iterator),
553-
Applicability::Unspecified,
556+
Applicability::HasPlaceholders,
554557
);
555558
}
556559
}
@@ -1006,7 +1009,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
10061009
let big_sugg = manual_copies
10071010
.into_iter()
10081011
.map(|(dst_var, src_var)| {
1009-
let start_str = Offset::positive(snippet_opt(cx, start.span).unwrap_or_else(|| "".into()));
1012+
let start_str = Offset::positive(snippet(cx, start.span, "").to_string());
10101013
let dst_offset = print_sum(&start_str, &dst_var.offset);
10111014
let dst_limit = print_limit(end, dst_var.offset, &dst_var.var_name);
10121015
let src_offset = print_sum(&start_str, &src_var.offset);
@@ -1305,7 +1308,8 @@ fn check_for_loop_reverse_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arg: &'tcx
13051308
}
13061309

13071310
fn lint_iter_method(cx: &LateContext<'_, '_>, args: &[Expr], arg: &Expr, method_name: &str) {
1308-
let object = snippet(cx, args[0].span, "_");
1311+
let mut applicability = Applicability::MachineApplicable;
1312+
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
13091313
let muta = if method_name == "iter_mut" {
13101314
"mut "
13111315
} else {
@@ -1319,7 +1323,7 @@ fn lint_iter_method(cx: &LateContext<'_, '_>, args: &[Expr], arg: &Expr, method_
13191323
iteration methods",
13201324
"to write this more concisely, try",
13211325
format!("&{}{}", muta, object),
1322-
Applicability::Unspecified,
1326+
applicability,
13231327
)
13241328
}
13251329

@@ -1349,7 +1353,8 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat, arg: &Expr, expr: &Ex
13491353
_ => lint_iter_method(cx, args, arg, method_name),
13501354
};
13511355
} else {
1352-
let object = snippet(cx, args[0].span, "_");
1356+
let mut applicability = Applicability::MachineApplicable;
1357+
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
13531358
span_lint_and_sugg(
13541359
cx,
13551360
EXPLICIT_INTO_ITER_LOOP,
@@ -1358,7 +1363,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat, arg: &Expr, expr: &Ex
13581363
iteration methods`",
13591364
"to write this more concisely, try",
13601365
object.to_string(),
1361-
Applicability::Unspecified,
1366+
applicability,
13621367
);
13631368
}
13641369
} else if method_name == "next" && match_trait_method(cx, arg, &paths::ITERATOR) {

clippy_lints/src/map_clone.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::rustc_errors::Applicability;
1515
use crate::syntax::ast::Ident;
1616
use crate::syntax::source_map::Span;
1717
use crate::utils::paths;
18-
use crate::utils::{in_macro, match_trait_method, match_type, remove_blocks, snippet, span_lint_and_sugg};
18+
use crate::utils::{in_macro, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
1919
use if_chain::if_chain;
2020

2121
#[derive(Clone)]
@@ -92,14 +92,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9292
fn lint(cx: &LateContext<'_, '_>, replace: Span, root: Span, name: Ident, path: &hir::Expr) {
9393
if let hir::ExprKind::Path(hir::QPath::Resolved(None, ref path)) = path.node {
9494
if path.segments.len() == 1 && path.segments[0].ident == name {
95+
let mut applicability = Applicability::MachineApplicable;
9596
span_lint_and_sugg(
9697
cx,
9798
MAP_CLONE,
9899
replace,
99100
"You are using an explicit closure for cloning elements",
100101
"Consider calling the dedicated `cloned` method",
101-
format!("{}.cloned()", snippet(cx, root, "..")),
102-
Applicability::Unspecified,
102+
format!("{}.cloned()", snippet_with_applicability(cx, root, "..", &mut applicability)),
103+
applicability,
103104
)
104105
}
105106
}

0 commit comments

Comments
 (0)