From 7270cdf35dd9da165c9e82ac39a600d0865fbf89 Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Fri, 20 Jul 2018 16:23:19 +0200 Subject: [PATCH 1/4] Change ELSE_IF_WITHOUT_ELSE from sugg to help lint --- clippy_lints/src/else_if_without_else.rs | 5 ++--- tests/ui/else_if_without_else.stderr | 7 +++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/else_if_without_else.rs b/clippy_lints/src/else_if_without_else.rs index 39404bbafcca..bdf95304075a 100644 --- a/clippy_lints/src/else_if_without_else.rs +++ b/clippy_lints/src/else_if_without_else.rs @@ -4,7 +4,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use syntax::ast::*; -use crate::utils::span_lint_and_sugg; +use crate::utils::span_help_and_lint; /// **What it does:** Checks for usage of if expressions with an `else if` branch, /// but without a final `else` branch. @@ -56,13 +56,12 @@ impl EarlyLintPass for ElseIfWithoutElse { while let ExprKind::If(_, _, Some(ref els)) = item.node { if let ExprKind::If(_, _, None) = els.node { - span_lint_and_sugg( + span_help_and_lint( cx, ELSE_IF_WITHOUT_ELSE, els.span, "if expression with an `else if`, but without a final `else`", "add an `else` block here", - "".to_string() ); } diff --git a/tests/ui/else_if_without_else.stderr b/tests/ui/else_if_without_else.stderr index b8a5031fbcff..b36430a0b341 100644 --- a/tests/ui/else_if_without_else.stderr +++ b/tests/ui/else_if_without_else.stderr @@ -5,9 +5,10 @@ error: if expression with an `else if`, but without a final `else` | ____________^ 40 | | println!("else if"); 41 | | } - | |_____^ help: add an `else` block here + | |_____^ | = note: `-D else-if-without-else` implied by `-D warnings` + = help: add an `else` block here error: if expression with an `else if`, but without a final `else` --> $DIR/else_if_without_else.rs:47:12 @@ -16,7 +17,9 @@ error: if expression with an `else if`, but without a final `else` | ____________^ 48 | | println!("else if 2"); 49 | | } - | |_____^ help: add an `else` block here + | |_____^ + | + = help: add an `else` block here error: aborting due to 2 previous errors From 50c2b715ea21635552f0096edf99f3dc7b8db368 Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Fri, 20 Jul 2018 16:33:02 +0200 Subject: [PATCH 2/4] Add applicability to span_lint_and_sugg --- clippy_lints/src/bytecount.rs | 4 +++- clippy_lints/src/collapsible_if.rs | 5 ++++- clippy_lints/src/default_trait_access.rs | 4 +++- clippy_lints/src/double_comparison.rs | 3 ++- clippy_lints/src/duration_subsec.rs | 2 ++ clippy_lints/src/excessive_precision.rs | 2 ++ .../src/infallible_destructuring_match.rs | 2 ++ clippy_lints/src/len_zero.rs | 2 ++ clippy_lints/src/literal_representation.rs | 5 +++++ clippy_lints/src/loops.rs | 6 ++++++ clippy_lints/src/matches.rs | 5 ++++- clippy_lints/src/methods.rs | 17 +++++++++++++++-- clippy_lints/src/needless_bool.rs | 6 ++++++ clippy_lints/src/no_effect.rs | 2 ++ clippy_lints/src/precedence.rs | 3 +++ clippy_lints/src/redundant_field_names.rs | 4 +++- clippy_lints/src/reference.rs | 5 ++++- clippy_lints/src/replace_consts.rs | 2 ++ clippy_lints/src/strings.rs | 2 ++ clippy_lints/src/trivially_copy_pass_by_ref.rs | 4 +++- clippy_lints/src/types.rs | 12 +++++++++--- clippy_lints/src/utils/mod.rs | 3 ++- clippy_lints/src/vec.rs | 2 ++ clippy_lints/src/write.rs | 3 +++ 24 files changed, 91 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/bytecount.rs b/clippy_lints/src/bytecount.rs index 2d4279d3cc19..2b4ae1688f5b 100644 --- a/clippy_lints/src/bytecount.rs +++ b/clippy_lints/src/bytecount.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty; +use rustc_errors::Applicability; use syntax::ast::{Name, UintTy}; use crate::utils::{contains_name, get_pat_name, match_type, paths, single_segment_path, snippet, span_lint_and_sugg, walk_ptrs_ty}; @@ -86,7 +87,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount { "Consider using the bytecount crate", format!("bytecount::count({}, {})", snippet(cx, haystack.span, ".."), - snippet(cx, needle.span, ".."))); + snippet(cx, needle.span, "..")), + Applicability::HasPlaceholders); } }; } diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs index 2771006aad3b..2dd93d3b6d2d 100644 --- a/clippy_lints/src/collapsible_if.rs +++ b/clippy_lints/src/collapsible_if.rs @@ -14,6 +14,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use if_chain::if_chain; use syntax::ast; @@ -114,7 +115,9 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) { block.span, "this `else { if .. }` block can be collapsed", "try", - snippet_block(cx, else_.span, "..").into_owned()); + snippet_block(cx, else_.span, "..").into_owned(), + Applicability::HasPlaceholders, + ); } _ => (), } diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index 4078237e8aa8..84ae20954f71 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::TypeVariants; +use rustc_errors::Applicability; use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg}; @@ -59,7 +60,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess { expr.span, &format!("Calling {} is more clear than this expression", replacement), "try", - replacement); + replacement, + Applicability::MaybeIncorrect); } }, QPath::TypeRelative(..) => {}, diff --git a/clippy_lints/src/double_comparison.rs b/clippy_lints/src/double_comparison.rs index 434ccb69921e..e6c6de691256 100644 --- a/clippy_lints/src/double_comparison.rs +++ b/clippy_lints/src/double_comparison.rs @@ -3,6 +3,7 @@ use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use syntax::codemap::Span; use crate::utils::{snippet, span_lint_and_sugg, SpanlessEq}; @@ -64,7 +65,7 @@ impl<'a, 'tcx> DoubleComparisonPass { let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str); span_lint_and_sugg(cx, DOUBLE_COMPARISONS, span, "This binary expression can be simplified", - "try", sugg); + "try", sugg, Applicability::HasPlaceholders); }} } match (op, lkind, rkind) { diff --git a/clippy_lints/src/duration_subsec.rs b/clippy_lints/src/duration_subsec.rs index 517befa7790f..805ed1d3dafa 100644 --- a/clippy_lints/src/duration_subsec.rs +++ b/clippy_lints/src/duration_subsec.rs @@ -1,6 +1,7 @@ use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use if_chain::if_chain; use syntax::codemap::Spanned; @@ -58,6 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec { &format!("Calling `{}()` is more concise than this calculation", suggested_fn), "try", format!("{}.{}()", snippet(cx, args[0].span, "_"), suggested_fn), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/excessive_precision.rs b/clippy_lints/src/excessive_precision.rs index 2c673fdfe3f7..29ac29755324 100644 --- a/clippy_lints/src/excessive_precision.rs +++ b/clippy_lints/src/excessive_precision.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::TypeVariants; +use rustc_errors::Applicability; use std::f32; use std::f64; use std::fmt; @@ -58,6 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision { "float has excessive precision", "consider changing the type or truncating it to", sugg, + Applicability::MachineApplicable, ); } } diff --git a/clippy_lints/src/infallible_destructuring_match.rs b/clippy_lints/src/infallible_destructuring_match.rs index 8b8cb32deb1a..fcb58bade58e 100644 --- a/clippy_lints/src/infallible_destructuring_match.rs +++ b/clippy_lints/src/infallible_destructuring_match.rs @@ -2,6 +2,7 @@ use super::utils::{get_arg_name, match_var, remove_blocks, snippet, span_lint_an use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use if_chain::if_chain; /// **What it does:** Checks for matches being used to destructure a single-variant enum @@ -74,6 +75,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { snippet(cx, local.pat.span, ".."), snippet(cx, target.span, ".."), ), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index b73f912fad5e..0ef703f7b914 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -3,6 +3,7 @@ use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; use rustc::ty; +use rustc_errors::Applicability; use std::collections::HashSet; use syntax::ast::{Lit, LitKind, Name}; use syntax::codemap::{Span, Spanned}; @@ -226,6 +227,7 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex &format!("length comparison to {}", if compare_to == 0 { "zero" } else { "one" }), "using `is_empty` is more concise", format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs index 45f9af49a15f..64d7e5950a24 100644 --- a/clippy_lints/src/literal_representation.rs +++ b/clippy_lints/src/literal_representation.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use if_chain::if_chain; use syntax::ast::*; use syntax_pos; @@ -238,6 +239,7 @@ impl WarningType { "long literal lacking separators", "consider", grouping_hint.to_owned(), + Applicability::MachineApplicable, ), WarningType::LargeDigitGroups => span_lint_and_sugg( cx, @@ -246,6 +248,7 @@ impl WarningType { "digit groups should be smaller", "consider", grouping_hint.to_owned(), + Applicability::MachineApplicable, ), WarningType::InconsistentDigitGrouping => span_lint_and_sugg( cx, @@ -254,6 +257,7 @@ impl WarningType { "digits grouped inconsistently by underscores", "consider", grouping_hint.to_owned(), + Applicability::MachineApplicable, ), WarningType::DecimalRepresentation => span_lint_and_sugg( cx, @@ -262,6 +266,7 @@ impl WarningType { "integer literal has a better hexadecimal representation", "consider", grouping_hint.to_owned(), + Applicability::MachineApplicable, ), }; } diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 85a9c13ff35c..8d737ae9313e 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -15,6 +15,7 @@ use rustc::middle::mem_categorization::Categorization; use rustc::middle::mem_categorization::cmt_; use rustc::ty::{self, Ty}; use rustc::ty::subst::Subst; +use rustc_errors::Applicability; use std::collections::{HashMap, HashSet}; use std::iter::{once, Iterator}; use syntax::ast; @@ -470,6 +471,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { snippet(cx, arms[0].pats[0].span, ".."), snippet(cx, matchexpr.span, "..") ), + Applicability::HasPlaceholders, ); } }, @@ -501,6 +503,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { "this loop could be written as a `for` loop", "try", format!("for {} in {} {{ .. }}", loop_var, iterator), + Applicability::HasPlaceholders, ); } } @@ -965,6 +968,7 @@ fn detect_manual_memcpy<'a, 'tcx>( "it looks like you're manually copying between slices", "try replacing the loop by", big_sugg, + Applicability::MachineApplicable, ); } } @@ -1200,6 +1204,7 @@ fn lint_iter_method(cx: &LateContext<'_, '_>, args: &[Expr], arg: &Expr, method_ iteration methods", "to write this more concisely, try", format!("&{}{}", muta, object), + Applicability::HasPlaceholders, ) } @@ -1238,6 +1243,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat, arg: &Expr, expr: &Ex iteration methods`", "to write this more concisely, try", object.to_string(), + Applicability::HasPlaceholders, ); } } else if method_name == "next" && match_trait_method(cx, arg, &paths::ITERATOR) { diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 6bdcd004134e..d6680f8b61e1 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::{self, Ty}; +use rustc_errors::Applicability; use std::cmp::Ordering; use std::collections::Bound; use syntax::ast::LitKind; @@ -249,6 +250,7 @@ fn report_single_match_single_pattern(cx: &LateContext<'_, '_>, ex: &Expr, arms: expr_block(cx, &arms[0].body, None, ".."), els_str ), + Applicability::HasPlaceholders, ); } @@ -455,7 +457,8 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: & expr.span, &format!("use {}() instead", suggestion), "try this", - format!("{}.{}()", snippet(cx, ex.span, "_"), suggestion) + format!("{}.{}()", snippet(cx, ex.span, "_"), suggestion), + Applicability::HasPlaceholders, ) } } diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index 28ff303fc83e..bd57be36000e 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -5,6 +5,7 @@ use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::{self, Ty}; use rustc::hir::def::Def; +use rustc_errors::Applicability; use std::borrow::Cow; use std::fmt; use std::iter; @@ -911,6 +912,7 @@ fn lint_or_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: Spa &format!("use of `{}` followed by a call to `{}`", name, path), "try this", format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")), + Applicability::HasPlaceholders ); return true; } @@ -980,6 +982,7 @@ fn lint_or_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: Spa &format!("use of `{}` followed by a function call", name), "try this", format!("{}_{}({})", name, suffix, sugg), + Applicability::MachineApplicable, ); } @@ -1074,6 +1077,7 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: &format!("use of `{}` followed by a function call", name), "try this", format!("unwrap_or_else({} panic!({}))", closure, sugg), + Applicability::HasPlaceholders, ); return; @@ -1088,6 +1092,7 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: &format!("use of `{}` followed by a function call", name), "try this", format!("unwrap_or_else({} panic!({}))", closure, sugg), + Applicability::HasPlaceholders, ); } @@ -1189,6 +1194,7 @@ fn lint_clone_on_ref_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir:: "using '.clone()' on a ref-counted pointer", "try this", format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet(cx, arg.span, "_")), + Applicability::HasPlaceholders, ); } } @@ -1219,6 +1225,7 @@ fn lint_string_extend(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::E ref_str, snippet(cx, target.span, "_") ), + Applicability::HasPlaceholders, ); } } @@ -1325,6 +1332,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: "this `.fold` can be written more succinctly using another method", "try", sugg, + Applicability::HasPlaceholders, ); } } @@ -1413,6 +1421,7 @@ fn lint_get_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, get_args: &[hir:: snippet(cx, get_args[0].span, "_"), snippet(cx, get_args[1].span, "_") ), + Applicability::HasPlaceholders, ); } @@ -1814,7 +1823,9 @@ fn lint_chars_cmp( if info.eq { "" } else { "!" }, snippet(cx, args[0][0].span, "_"), suggest, - snippet(cx, arg_char[0].span, "_"))); + snippet(cx, arg_char[0].span, "_")), + Applicability::HasPlaceholders, + ); return true; } @@ -1860,7 +1871,8 @@ fn lint_chars_cmp_with_unwrap<'a, 'tcx>( if info.eq { "" } else { "!" }, snippet(cx, args[0][0].span, "_"), suggest, - c) + c), + Applicability::HasPlaceholders, ); return true; @@ -1925,6 +1937,7 @@ fn lint_asref(cx: &LateContext<'_, '_>, expr: &hir::Expr, call_name: &str, as_re &format!("this call to `{}` does nothing", call_name), "try this", snippet(cx, recvr.span, "_").into_owned(), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index 559aa74f9a22..0f46e7b451e3 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -5,6 +5,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use rustc::hir::*; +use rustc_errors::Applicability; use syntax::ast::LitKind; use syntax::codemap::Spanned; use crate::utils::{snippet, span_lint, span_lint_and_sugg}; @@ -79,6 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool { "this if-then-else expression returns a bool literal", "you can reduce it to", hint, + Applicability::HasPlaceholders, ); }; if let ExprKind::Block(ref then_block, _) = then_block.node { @@ -135,6 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { "equality checks against true are unnecessary", "try simplifying it as shown", hint, + Applicability::HasPlaceholders, ); }, (Other, Bool(true)) => { @@ -146,6 +149,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { "equality checks against true are unnecessary", "try simplifying it as shown", hint, + Applicability::HasPlaceholders, ); }, (Bool(false), Other) => { @@ -157,6 +161,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { "equality checks against false can be replaced by a negation", "try simplifying it as shown", (!hint).to_string(), + Applicability::HasPlaceholders, ); }, (Other, Bool(false)) => { @@ -168,6 +173,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { "equality checks against false can be replaced by a negation", "try simplifying it as shown", (!hint).to_string(), + Applicability::HasPlaceholders, ); }, _ => (), diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index cacb5d6a9ffc..657a68573089 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -2,6 +2,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint, lint_array}; use rustc::hir::def::Def; use rustc::hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource}; +use rustc_errors::Applicability; use crate::utils::{has_drop, in_macro, snippet_opt, span_lint, span_lint_and_sugg}; use std::ops::Deref; @@ -121,6 +122,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { "statement can be reduced", "replace it with", snippet, + Applicability::MachineApplicable, ); } } diff --git a/clippy_lints/src/precedence.rs b/clippy_lints/src/precedence.rs index 6a0f4f147b72..7f121e0322af 100644 --- a/clippy_lints/src/precedence.rs +++ b/clippy_lints/src/precedence.rs @@ -1,5 +1,6 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use syntax::ast::*; use syntax::codemap::Spanned; use crate::utils::{in_macro, snippet, span_lint_and_sugg}; @@ -51,6 +52,7 @@ impl EarlyLintPass for Precedence { "operator precedence can trip the unwary", "consider parenthesizing your expression", sugg, + Applicability::HasPlaceholders, ); }; @@ -102,6 +104,7 @@ impl EarlyLintPass for Precedence { "unary minus has lower precedence than method call", "consider adding parentheses to clarify your intent", format!("-({})", snippet(cx, rhs.span, "..")), + Applicability::HasPlaceholders, ); }, _ => (), diff --git a/clippy_lints/src/redundant_field_names.rs b/clippy_lints/src/redundant_field_names.rs index 4f28d36e2a8f..cad5256a1be6 100644 --- a/clippy_lints/src/redundant_field_names.rs +++ b/clippy_lints/src/redundant_field_names.rs @@ -1,6 +1,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use rustc::hir::*; +use rustc_errors::Applicability; use crate::utils::{in_macro, is_range_expression, match_var, span_lint_and_sugg}; /// **What it does:** Checks for fields in struct literals where shorthands @@ -55,7 +56,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames { field.span, "redundant field names in struct initialization", "replace it with", - name.to_string() + name.to_string(), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/reference.rs b/clippy_lints/src/reference.rs index f349f46d926e..8958ef02bffd 100644 --- a/clippy_lints/src/reference.rs +++ b/clippy_lints/src/reference.rs @@ -1,6 +1,7 @@ use syntax::ast::{Expr, ExprKind, UnOp}; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use if_chain::if_chain; use crate::utils::{snippet, span_lint_and_sugg}; @@ -51,6 +52,7 @@ impl EarlyLintPass for Pass { "immediately dereferencing a reference", "try this", format!("{}", snippet(cx, addrof_target.span, "_")), + Applicability::HasPlaceholders, ); } } @@ -100,7 +102,8 @@ impl EarlyLintPass for DerefPass { "{}.{}", snippet(cx, inner.span, "_"), snippet(cx, field_name.span, "_") - ) + ), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/replace_consts.rs b/clippy_lints/src/replace_consts.rs index b9a4c6ebb195..94f1e3d87e50 100644 --- a/clippy_lints/src/replace_consts.rs +++ b/clippy_lints/src/replace_consts.rs @@ -3,6 +3,7 @@ use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::hir; use rustc::hir::def::Def; +use rustc_errors::Applicability; use crate::utils::{match_def_path, span_lint_and_sugg}; /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and @@ -51,6 +52,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts { &format!("using `{}`", const_path.last().expect("empty path")), "try this", repl_snip.to_string(), + Applicability::MachineApplicable, ); return; } diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index a13f864c5ce7..10eb8f5837f7 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -1,6 +1,7 @@ use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use syntax::codemap::Spanned; use crate::utils::SpanlessEq; use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty}; @@ -161,6 +162,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { "calling `as_bytes()` on a string literal", "consider using a byte string literal instead", format!("b{}", snippet(cx, args[0].span, r#""foo""#)), + Applicability::HasPlaceholders, ); } } diff --git a/clippy_lints/src/trivially_copy_pass_by_ref.rs b/clippy_lints/src/trivially_copy_pass_by_ref.rs index 6a048b192138..180ad368a215 100644 --- a/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -9,6 +9,7 @@ use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::TypeVariants; use rustc::session::config::Config as SessionConfig; +use rustc_errors::Applicability; use rustc_target::spec::abi::Abi; use rustc_target::abi::LayoutOf; use syntax::ast::NodeId; @@ -155,7 +156,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { input.span, "this argument is passed by reference, but would be more efficient if passed by value", "consider passing by value instead", - value_type); + value_type, + Applicability::HasPlaceholders); } } } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 7b3f6f20fc74..0f90272ddf4f 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -7,6 +7,7 @@ use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::{self, Ty, TyCtxt, TypeckTables}; use rustc::ty::layout::LayoutOf; +use rustc_errors::Applicability; use rustc_typeck::hir_ty_to_ty; use std::cmp::Ordering; use std::collections::BTreeMap; @@ -331,7 +332,8 @@ fn check_ty_rptr(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool, lt: ast_ty.span, "you seem to be trying to use `&Box`. Consider using just `&T`", "try", - format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")) + format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")), + Applicability::HasPlaceholders, ); return; // don't recurse into the type } @@ -527,6 +529,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg { "passing a unit value to a function", "if you intended to pass a unit value, use a unit literal instead", "()".to_string(), + Applicability::MaybeIncorrect, ); } } @@ -845,6 +848,7 @@ fn span_lossless_lint(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro &format!("casting {} to {} may become silently lossy if types change", cast_from, cast_to), "try", format!("{}::from({})", cast_to, sugg), + Applicability::HasPlaceholders, ); } @@ -1044,7 +1048,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass { expr.span, &format!("casting a `{}` to `{}` may truncate the function address value.", cast_from, cast_to), "if you need the address of the function, consider", - format!("{} as usize", &snippet(cx, ex.span, "x")) + format!("{} as usize", &snippet(cx, ex.span, "x")), + Applicability::HasPlaceholders, ); } else { span_lint_and_sugg( @@ -1053,7 +1058,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass { expr.span, &format!("casting a `{}` to `{}` is bad style.", cast_from, cast_to), "if you need the address of the function, consider", - format!("{} as usize", &snippet(cx, ex.span, "x")) + format!("{} as usize", &snippet(cx, ex.span, "x")), + Applicability::HasPlaceholders, ); }; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 0b2103ca7eaa..eb45ba1e066b 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -600,9 +600,10 @@ pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>( msg: &str, help: &str, sugg: String, + applicability: Applicability, ) { span_lint_and_then(cx, lint, sp, msg, |db| { - db.span_suggestion(sp, help, sugg); + db.span_suggestion_with_applicability(sp, help, sugg, applicability); }); } diff --git a/clippy_lints/src/vec.rs b/clippy_lints/src/vec.rs index cea3307a8273..0384a411a42f 100644 --- a/clippy_lints/src/vec.rs +++ b/clippy_lints/src/vec.rs @@ -3,6 +3,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; use if_chain::if_chain; use rustc::ty::{self, Ty}; +use rustc_errors::Applicability; use syntax::codemap::Span; use crate::utils::{higher, is_copy, snippet, span_lint_and_sugg}; use crate::consts::constant; @@ -90,6 +91,7 @@ fn check_vec_macro<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, vec_args: &higher::VecA "useless use of `vec!`", "you can use a slice directly", snippet, + Applicability::HasPlaceholders, ); } diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 0b52981bfa58..812f3bc51a22 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -1,5 +1,6 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use syntax::ast::*; use syntax::tokenstream::{ThinTokenStream, TokenStream}; use syntax::parse::{token, parser}; @@ -180,6 +181,7 @@ impl EarlyLintPass for Pass { "using `println!(\"\")`", "replace it with", "println!()".to_string(), + Applicability::HasPlaceholders, ); } } @@ -210,6 +212,7 @@ impl EarlyLintPass for Pass { "using `writeln!(v, \"\")`", "replace it with", "writeln!(v)".to_string(), + Applicability::HasPlaceholders, ); } } From 5d6ce230691152f66a581dbf851f3f15676a8c1f Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Fri, 20 Jul 2018 16:34:02 +0200 Subject: [PATCH 3/4] Add applicability to multispan_sugg --- clippy_lints/src/eq_op.rs | 2 ++ clippy_lints/src/loops.rs | 3 +++ clippy_lints/src/matches.rs | 2 +- clippy_lints/src/needless_pass_by_value.rs | 7 ++++++- clippy_lints/src/types.rs | 7 ++++++- clippy_lints/src/utils/mod.rs | 10 +++++++--- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/eq_op.rs b/clippy_lints/src/eq_op.rs index dfbc3b126336..f60daffb333b 100644 --- a/clippy_lints/src/eq_op.rs +++ b/clippy_lints/src/eq_op.rs @@ -1,6 +1,7 @@ use rustc::hir::*; use rustc::lint::*; use rustc::{declare_lint, lint_array}; +use rustc_errors::Applicability; use crate::utils::{in_macro, implements_trait, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq}; /// **What it does:** Checks for equal operands to comparison, logical and @@ -107,6 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp { db, "use the values directly".to_string(), vec![(left.span, lsnip), (right.span, rsnip)], + Applicability::HasPlaceholders, ); }, ) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 8d737ae9313e..8011d7d335cf 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1073,6 +1073,7 @@ fn check_for_loop_range<'a, 'tcx>( (pat.span, format!("({}, )", ident.name)), (arg.span, format!("{}.{}().enumerate(){}{}", indexed, method, take, skip)), ], + Applicability::HasPlaceholders, ); }, ); @@ -1093,6 +1094,7 @@ fn check_for_loop_range<'a, 'tcx>( db, "consider using an iterator".to_string(), vec![(pat.span, "".to_string()), (arg.span, repl)], + Applicability::HasPlaceholders, ); }, ); @@ -1404,6 +1406,7 @@ fn check_for_loop_over_map_kv<'a, 'tcx>( (pat_span, snippet(cx, new_pat_span, kind).into_owned()), (arg_span, format!("{}.{}s{}()", map.maybe_par(), kind, mutbl)), ], + Applicability::HasPlaceholders, ); }, ); diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index d6680f8b61e1..93250735e951 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -433,7 +433,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: })); span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |db| { - multispan_sugg(db, msg.to_owned(), suggs); + multispan_sugg(db, msg.to_owned(), suggs, Applicability::HasPlaceholders); }); } } diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 82e85f3453a1..6955a9eff89a 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -9,6 +9,7 @@ use rustc::ty::{self, RegionKind, TypeFoldable}; use rustc::traits; use rustc::middle::expr_use_visitor as euv; use rustc::middle::mem_categorization as mc; +use rustc_errors::Applicability; use rustc_target::spec::abi::Abi; use syntax::ast::NodeId; use syntax_pos::Span; @@ -284,7 +285,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { ); spans.sort_by_key(|&(span, _)| span); } - multispan_sugg(db, "consider taking a reference instead".to_string(), spans); + multispan_sugg(db, + "consider taking a reference instead".to_string(), + spans, + Applicability::HasPlaceholders, + ); }; span_lint_and_then( diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 0f90272ddf4f..32562e4073bb 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1793,10 +1793,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher { format!("{}<{}, S>", target.type_name(), target.type_arguments(),), ), ], + Applicability::HasPlaceholders, ); if !vis.suggestions.is_empty() { - multispan_sugg(db, "...and use generic constructor".into(), vis.suggestions); + multispan_sugg(db, + "...and use generic constructor".into(), + vis.suggestions, + Applicability::HasPlaceholders, + ); } } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index eb45ba1e066b..f604bb1e9da0 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -613,8 +613,12 @@ pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>( /// appear once per /// replacement. In human-readable format though, it only appears once before /// the whole suggestion. -pub fn multispan_sugg(db: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I) -where +pub fn multispan_sugg( + db: &mut DiagnosticBuilder, + help_msg: String, + sugg: I, + applicability: Applicability, +) where I: IntoIterator, { let sugg = CodeSuggestion { @@ -632,7 +636,7 @@ where ], msg: help_msg, show_code_when_inline: true, - applicability: Applicability::Unspecified, + applicability, }; db.suggestions.push(sugg); } From 5ae7cfa61d94eae8ba819a13f5a8bd9a8be676d3 Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Thu, 26 Jul 2018 12:51:58 +0200 Subject: [PATCH 4/4] Fixing rebase error --- clippy_lints/src/utils/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index f604bb1e9da0..54554af1f229 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -614,7 +614,7 @@ pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>( /// replacement. In human-readable format though, it only appears once before /// the whole suggestion. pub fn multispan_sugg( - db: &mut DiagnosticBuilder, + db: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I, applicability: Applicability,