Skip to content

Commit b2601be

Browse files
authoredNov 27, 2018
Merge pull request #3459 from flip1995/sugg_appl
Add Applicability to suggestion lints: Take 2
2 parents dec389a + 87e72a5 commit b2601be

33 files changed

+545
-295
lines changed
 

‎clippy_lints/src/attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +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::MachineApplicable,
535536
);
536537
}
537538
}

‎clippy_lints/src/bytecount.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
use crate::rustc::hir::*;
1212
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13-
use crate::rustc::{declare_tool_lint, lint_array};
14-
use if_chain::if_chain;
1513
use crate::rustc::ty;
14+
use crate::rustc::{declare_tool_lint, lint_array};
15+
use crate::rustc_errors::Applicability;
1616
use crate::syntax::ast::{Name, UintTy};
17-
use crate::utils::{contains_name, get_pat_name, match_type, paths, single_segment_path, snippet, span_lint_and_sugg,
18-
walk_ptrs_ty};
17+
use crate::utils::{
18+
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability,
19+
span_lint_and_sugg, walk_ptrs_ty,
20+
};
21+
use if_chain::if_chain;
1922

2023
/// **What it does:** Checks for naive byte counts
2124
///
@@ -89,14 +92,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
8992
} else {
9093
&filter_args[0]
9194
};
92-
span_lint_and_sugg(cx,
93-
NAIVE_BYTECOUNT,
94-
expr.span,
95-
"You appear to be counting bytes the naive way",
96-
"Consider using the bytecount crate",
97-
format!("bytecount::count({}, {})",
98-
snippet(cx, haystack.span, ".."),
99-
snippet(cx, needle.span, "..")));
95+
let mut applicability = Applicability::MachineApplicable;
96+
span_lint_and_sugg(
97+
cx,
98+
NAIVE_BYTECOUNT,
99+
expr.span,
100+
"You appear to be counting bytes the naive way",
101+
"Consider using the bytecount crate",
102+
format!("bytecount::count({}, {})",
103+
snippet_with_applicability(cx, haystack.span, "..", &mut applicability),
104+
snippet_with_applicability(cx, needle.span, "..", &mut applicability)),
105+
applicability,
106+
);
100107
}
101108
};
102109
}

‎clippy_lints/src/collapsible_if.rs

+11-7
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,12 +128,16 @@ 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-
span_lint_and_sugg(cx,
132-
COLLAPSIBLE_IF,
133-
block.span,
134-
"this `else { if .. }` block can be collapsed",
135-
"try",
136-
snippet_block(cx, else_.span, "..").into_owned());
131+
let mut applicability = Applicability::MachineApplicable;
132+
span_lint_and_sugg(
133+
cx,
134+
COLLAPSIBLE_IF,
135+
block.span,
136+
"this `else { if .. }` block can be collapsed",
137+
"try",
138+
snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(),
139+
applicability,
140+
);
137141
}
138142
_ => (),
139143
}

‎clippy_lints/src/default_trait_access.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
use crate::rustc::hir::*;
1212
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13+
use crate::rustc::ty::TyKind;
1314
use crate::rustc::{declare_tool_lint, lint_array};
15+
use crate::rustc_errors::Applicability;
1416
use if_chain::if_chain;
15-
use crate::rustc::ty::TyKind;
1617

1718
use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg};
1819

@@ -80,7 +81,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
8081
expr.span,
8182
&format!("Calling {} is more clear than this expression", replacement),
8283
"try",
83-
replacement);
84+
replacement,
85+
Applicability::Unspecified, // First resolve the TODO above
86+
);
8487
}
8588
},
8689
QPath::TypeRelative(..) => {},

‎clippy_lints/src/double_comparison.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use crate::rustc::hir::*;
1414
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1515
use crate::rustc::{declare_tool_lint, lint_array};
16+
use crate::rustc_errors::Applicability;
1617
use crate::syntax::source_map::Span;
1718

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

2021
/// **What it does:** Checks for double comparions that could be simpified to a single expression.
2122
///
@@ -70,12 +71,19 @@ impl<'a, 'tcx> Pass {
7071
}
7172
macro_rules! lint_double_comparison {
7273
($op:tt) => {{
73-
let lhs_str = snippet(cx, llhs.span, "");
74-
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);
7577
let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
76-
span_lint_and_sugg(cx, DOUBLE_COMPARISONS, span,
77-
"This binary expression can be simplified",
78-
"try", sugg);
78+
span_lint_and_sugg(
79+
cx,
80+
DOUBLE_COMPARISONS,
81+
span,
82+
"This binary expression can be simplified",
83+
"try",
84+
sugg,
85+
applicability,
86+
);
7987
}}
8088
}
8189
match (op, lkind, rkind) {

‎clippy_lints/src/duration_subsec.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use crate::rustc::hir::*;
1212
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1313
use crate::rustc::{declare_tool_lint, lint_array};
14-
use if_chain::if_chain;
14+
use crate::rustc_errors::Applicability;
1515
use crate::syntax::source_map::Spanned;
16+
use if_chain::if_chain;
1617

1718
use crate::consts::{constant, Constant};
1819
use crate::utils::paths;
19-
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};
2021

2122
/// **What it does:** Checks for calculation of subsecond microseconds or milliseconds
2223
/// from other `Duration` methods.
@@ -60,13 +61,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
6061
("subsec_nanos", 1_000) => "subsec_micros",
6162
_ => return,
6263
};
64+
let mut applicability = Applicability::MachineApplicable;
6365
span_lint_and_sugg(
6466
cx,
6567
DURATION_SUBSEC,
6668
expr.span,
6769
&format!("Calling `{}()` is more concise than this calculation", suggested_fn),
6870
"try",
69-
format!("{}.{}()", snippet(cx, args[0].span, "_"), suggested_fn),
71+
format!("{}.{}()", snippet_with_applicability(cx, args[0].span, "_", &mut applicability), suggested_fn),
72+
applicability,
7073
);
7174
}
7275
}

‎clippy_lints/src/else_if_without_else.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass, in_ex
1414
use crate::rustc::{declare_tool_lint, lint_array};
1515
use crate::syntax::ast::*;
1616

17-
use crate::utils::span_lint_and_sugg;
17+
use crate::utils::span_help_and_lint;
1818

1919
/// **What it does:** Checks for usage of if expressions with an `else if` branch,
2020
/// but without a final `else` branch.
@@ -66,13 +66,12 @@ impl EarlyLintPass for ElseIfWithoutElse {
6666

6767
while let ExprKind::If(_, _, Some(ref els)) = item.node {
6868
if let ExprKind::If(_, _, None) = els.node {
69-
span_lint_and_sugg(
69+
span_help_and_lint(
7070
cx,
7171
ELSE_IF_WITHOUT_ELSE,
7272
els.span,
7373
"if expression with an `else if`, but without a final `else`",
7474
"add an `else` block here",
75-
String::new()
7675
);
7776
}
7877

‎clippy_lints/src/excessive_precision.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
use crate::rustc::hir;
1212
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13+
use crate::rustc::ty::TyKind;
1314
use crate::rustc::{declare_tool_lint, lint_array};
15+
use crate::rustc_errors::Applicability;
16+
use crate::syntax::ast::*;
17+
use crate::syntax_pos::symbol::Symbol;
18+
use crate::utils::span_lint_and_sugg;
1419
use if_chain::if_chain;
15-
use crate::rustc::ty::TyKind;
1620
use std::f32;
1721
use std::f64;
1822
use std::fmt;
19-
use crate::syntax::ast::*;
20-
use crate::syntax_pos::symbol::Symbol;
21-
use crate::utils::span_lint_and_sugg;
2223

2324
/// **What it does:** Checks for float literals with a precision greater
2425
/// than that supported by the underlying type
@@ -68,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
6869
"float has excessive precision",
6970
"consider changing the type or truncating it to",
7071
sugg,
72+
Applicability::MachineApplicable,
7173
);
7274
}
7375
}

‎clippy_lints/src/infallible_destructuring_match.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
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};
15+
use crate::rustc_errors::Applicability;
1516
use if_chain::if_chain;
1617

1718
/// **What it does:** Checks for matches being used to destructure a single-variant enum
@@ -71,6 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7172
if match_var(body, arg);
7273

7374
then {
75+
let mut applicability = Applicability::MachineApplicable;
7476
span_lint_and_sugg(
7577
cx,
7678
INFALLIBLE_DESTRUCTURING_MATCH,
@@ -80,10 +82,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8082
"try this",
8183
format!(
8284
"let {}({}) = {};",
83-
snippet(cx, variant_name.span, ".."),
84-
snippet(cx, local.pat.span, ".."),
85-
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),
8688
),
89+
applicability,
8790
);
8891
}
8992
}

‎clippy_lints/src/len_zero.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use crate::rustc::hir::def_id::DefId;
1212
use crate::rustc::hir::*;
1313
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14-
use crate::rustc::{declare_tool_lint, lint_array};
1514
use crate::rustc::ty;
15+
use crate::rustc::{declare_tool_lint, lint_array};
1616
use crate::rustc_data_structures::fx::FxHashSet;
17+
use crate::rustc_errors::Applicability;
1718
use crate::syntax::ast::{Lit, LitKind, Name};
1819
use crate::syntax::source_map::{Span, Spanned};
19-
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};
2021

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

226-
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+
) {
227236
if let Spanned {
228237
node: LitKind::Int(lit, _),
229238
..
@@ -235,13 +244,15 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex
235244
}
236245

237246
if method_name == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
247+
let mut applicability = Applicability::MachineApplicable;
238248
span_lint_and_sugg(
239249
cx,
240250
LEN_ZERO,
241251
span,
242252
&format!("length comparison to {}", if compare_to == 0 { "zero" } else { "one" }),
243253
"using `is_empty` is clearer and more explicit",
244-
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")),
254+
format!("{}{}.is_empty()", op, snippet_with_applicability(cx, args[0].span, "_", &mut applicability)),
255+
applicability,
245256
);
246257
}
247258
}

‎clippy_lints/src/literal_representation.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use crate::rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
1414
use crate::rustc::{declare_tool_lint, lint_array};
15+
use crate::rustc_errors::Applicability;
1516
use crate::syntax::ast::*;
1617
use crate::syntax_pos;
1718
use crate::utils::{snippet_opt, span_lint_and_sugg};
@@ -300,6 +301,7 @@ impl WarningType {
300301
"mistyped literal suffix",
301302
"did you mean to write",
302303
grouping_hint.to_string(),
304+
Applicability::MaybeIncorrect,
303305
),
304306
WarningType::UnreadableLiteral => span_lint_and_sugg(
305307
cx,
@@ -308,6 +310,7 @@ impl WarningType {
308310
"long literal lacking separators",
309311
"consider",
310312
grouping_hint.to_owned(),
313+
Applicability::MachineApplicable,
311314
),
312315
WarningType::LargeDigitGroups => span_lint_and_sugg(
313316
cx,
@@ -316,6 +319,7 @@ impl WarningType {
316319
"digit groups should be smaller",
317320
"consider",
318321
grouping_hint.to_owned(),
322+
Applicability::MachineApplicable,
319323
),
320324
WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
321325
cx,
@@ -324,6 +328,7 @@ impl WarningType {
324328
"digits grouped inconsistently by underscores",
325329
"consider",
326330
grouping_hint.to_owned(),
331+
Applicability::MachineApplicable,
327332
),
328333
WarningType::DecimalRepresentation => span_lint_and_sugg(
329334
cx,
@@ -332,6 +337,7 @@ impl WarningType {
332337
"integer literal has a better hexadecimal representation",
333338
"consider",
334339
grouping_hint.to_owned(),
340+
Applicability::MachineApplicable,
335341
),
336342
};
337343
}

0 commit comments

Comments
 (0)
Please sign in to comment.