diff --git a/clippy_lints/src/as_conversions.rs b/clippy_lints/src/as_conversions.rs index 4d8bbcd3102..0c8efd75514 100644 --- a/clippy_lints/src/as_conversions.rs +++ b/clippy_lints/src/as_conversions.rs @@ -50,6 +50,7 @@ impl EarlyLintPass for AsConversions { AS_CONVERSIONS, expr.span, "using a potentially dangerous silent `as` conversion", + None, "consider using a safe wrapper for this conversion", ); } diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index e2fe5f2400f..f8a8fdcd3aa 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -41,6 +41,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { } else { "`assert!(true)` will be optimized out by the compiler" }, + None, "remove it", ); }; @@ -50,6 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { ASSERTIONS_ON_CONSTANTS, e.span, "`assert!(false)` should probably be replaced", + None, "use `panic!()` or `unreachable!()`", ); }; @@ -59,6 +61,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { ASSERTIONS_ON_CONSTANTS, e.span, &format!("`assert!(false, {})` should probably be replaced", panic_message), + None, &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message), ) }; diff --git a/clippy_lints/src/atomic_ordering.rs b/clippy_lints/src/atomic_ordering.rs index d9ff1fe0a1d..73b4cef4725 100644 --- a/clippy_lints/src/atomic_ordering.rs +++ b/clippy_lints/src/atomic_ordering.rs @@ -85,6 +85,7 @@ fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { INVALID_ATOMIC_ORDERING, ordering_arg.span, "atomic loads cannot have `Release` and `AcqRel` ordering", + None, "consider using ordering modes `Acquire`, `SeqCst` or `Relaxed`" ); } else if method == "store" && @@ -94,6 +95,7 @@ fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { INVALID_ATOMIC_ORDERING, ordering_arg.span, "atomic stores cannot have `Acquire` and `AcqRel` ordering", + None, "consider using ordering modes `Release`, `SeqCst` or `Relaxed`" ); } @@ -118,6 +120,7 @@ fn check_memory_fence(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { INVALID_ATOMIC_ORDERING, args[0].span, "memory fences cannot have `Relaxed` ordering", + None, "consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`" ); } diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index 93a394b79e5..e842388ac98 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -105,6 +105,7 @@ impl CognitiveComplexity { rust_cc, self.limit.limit() ), + None, "you could split it up into multiple smaller functions", ); } diff --git a/clippy_lints/src/comparison_chain.rs b/clippy_lints/src/comparison_chain.rs index e0c381ddbfc..96df3ffe3ce 100644 --- a/clippy_lints/src/comparison_chain.rs +++ b/clippy_lints/src/comparison_chain.rs @@ -104,6 +104,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ComparisonChain { COMPARISON_CHAIN, expr.span, "`if` chain can be rewritten with `match`", + None, "Consider rewriting the `if` chain to use `cmp` and `match`.", ) } diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index f9bf1141a8b..e513dcce64e 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -48,6 +48,7 @@ impl EarlyLintPass for DbgMacro { DBG_MACRO, mac.span(), "`dbg!` macro is intended as a debugging tool", + None, "ensure to avoid having uses of it in version control", ); } diff --git a/clippy_lints/src/else_if_without_else.rs b/clippy_lints/src/else_if_without_else.rs index fb10ca48074..95123e6ff6f 100644 --- a/clippy_lints/src/else_if_without_else.rs +++ b/clippy_lints/src/else_if_without_else.rs @@ -61,6 +61,7 @@ impl EarlyLintPass for ElseIfWithoutElse { ELSE_IF_WITHOUT_ELSE, els.span, "`if` expression with an `else if`, but without a final `else`", + None, "add an `else` block here", ); } diff --git a/clippy_lints/src/empty_enum.rs b/clippy_lints/src/empty_enum.rs index 82c0bf93a7f..2c8721ae505 100644 --- a/clippy_lints/src/empty_enum.rs +++ b/clippy_lints/src/empty_enum.rs @@ -45,13 +45,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum { let ty = cx.tcx.type_of(did); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); if adt.variants.is_empty() { - span_lint_and_then( + span_lint_and_help( cx, EMPTY_ENUM, item.span, "enum with no variants", - "consider using the uninhabited type `!` (never type) or a wrapper around it \ - to introduce a type which can't be instantiated", + Some(item.span), + "consider using the uninhabited type `!` (never type) or a wrapper \ + around it to introduce a type which can't be instantiated", ); } } diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 882020a7adc..a5871cf0cd4 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -206,6 +206,7 @@ fn check_variant( lint, span, &format!("All variants have the same {}fix: `{}`", what, value), + None, &format!( "remove the {}fixes and use full paths to \ the variants instead of glob imports", diff --git a/clippy_lints/src/excessive_bools.rs b/clippy_lints/src/excessive_bools.rs index ddbc3c377a2..82ca4baacb7 100644 --- a/clippy_lints/src/excessive_bools.rs +++ b/clippy_lints/src/excessive_bools.rs @@ -114,6 +114,7 @@ impl ExcessiveBools { FN_PARAMS_EXCESSIVE_BOOLS, span, &format!("more than {} bools in function parameters", self.max_fn_params_bools), + None, "consider refactoring bools into two-variant enums", ); } @@ -153,6 +154,7 @@ impl EarlyLintPass for ExcessiveBools { STRUCT_EXCESSIVE_BOOLS, item.span, &format!("more than {} bools in a struct", self.max_struct_bools), + None, "consider using a state machine or refactoring bools into two-variant enums", ); } diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index 8f5f82b0a2c..d2ee22e8706 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -188,6 +188,7 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) { binop = binop_str, unop = unop_str ), + None, &format!( "put a space between `{binop}` and `{unop}` and remove the space after `{unop}`", binop = binop_str, diff --git a/clippy_lints/src/functions.rs b/clippy_lints/src/functions.rs index 7100bad996c..c8c562fe29f 100644 --- a/clippy_lints/src/functions.rs +++ b/clippy_lints/src/functions.rs @@ -431,6 +431,7 @@ fn check_needless_must_use( DOUBLE_MUST_USE, fn_header_span, "this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`", + None, "either add some descriptive text or remove the attribute", ); } diff --git a/clippy_lints/src/if_not_else.rs b/clippy_lints/src/if_not_else.rs index 271df5b03e3..c11e291f98e 100644 --- a/clippy_lints/src/if_not_else.rs +++ b/clippy_lints/src/if_not_else.rs @@ -61,6 +61,7 @@ impl EarlyLintPass for IfNotElse { IF_NOT_ELSE, item.span, "Unnecessary boolean `not` operation", + None, "remove the `!` and swap the blocks of the `if`/`else`", ); }, @@ -70,6 +71,7 @@ impl EarlyLintPass for IfNotElse { IF_NOT_ELSE, item.span, "Unnecessary `!=` operation", + None, "change to `==` and swap the blocks of the `if`/`else`", ); }, diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index a2b1085a36e..c5808dd540b 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -138,7 +138,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { (None, None) => return, // [..] is ok. }; - span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg); + span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic.", None, help_msg); } else { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind { @@ -154,6 +154,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { INDEXING_SLICING, expr.span, "indexing may panic.", + None, "Consider using `.get(n)` or `.get_mut(n)` instead", ); } diff --git a/clippy_lints/src/inherent_to_string.rs b/clippy_lints/src/inherent_to_string.rs index ca8e834e347..e343d690f6c 100644 --- a/clippy_lints/src/inherent_to_string.rs +++ b/clippy_lints/src/inherent_to_string.rs @@ -137,6 +137,7 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) { "type `{}` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`", self_type.to_string() ), + None, &format!("remove the inherent method from type `{}`", self_type.to_string()) ); } else { @@ -148,6 +149,7 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) { "implementation of inherent method `to_string(&self) -> String` for type `{}`", self_type.to_string() ), + None, &format!("implement trait `Display` for type `{}` instead", self_type.to_string()), ); } diff --git a/clippy_lints/src/integer_division.rs b/clippy_lints/src/integer_division.rs index 053d66e6af7..fe34d33fe65 100644 --- a/clippy_lints/src/integer_division.rs +++ b/clippy_lints/src/integer_division.rs @@ -35,6 +35,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntegerDivision { INTEGER_DIVISION, expr.span, "integer division", + None, "division of integers may cause loss of precision. consider using floats.", ); } diff --git a/clippy_lints/src/large_stack_arrays.rs b/clippy_lints/src/large_stack_arrays.rs index f67fce9697a..deb57db1678 100644 --- a/clippy_lints/src/large_stack_arrays.rs +++ b/clippy_lints/src/large_stack_arrays.rs @@ -57,6 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeStackArrays { "allocating a local array larger than {} bytes", self.maximum_allowed_size ), + None, &format!( "consider allocating on the heap with `vec!{}.into_boxed_slice()`", snippet(cx, expr.span, "[...]") diff --git a/clippy_lints/src/let_underscore.rs b/clippy_lints/src/let_underscore.rs index f8f84f3d42d..710dec8d33f 100644 --- a/clippy_lints/src/let_underscore.rs +++ b/clippy_lints/src/let_underscore.rs @@ -90,6 +90,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore { LET_UNDERSCORE_LOCK, local.span, "non-binding let on a synchronization lock", + None, "consider using an underscore-prefixed named \ binding or dropping explicitly with `std::mem::drop`" ) @@ -99,6 +100,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore { LET_UNDERSCORE_MUST_USE, local.span, "non-binding let on an expression with `#[must_use]` type", + None, "consider explicitly using expression value" ) } else if is_must_use_func_call(cx, init) { @@ -107,6 +109,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore { LET_UNDERSCORE_MUST_USE, local.span, "non-binding let on a result of a `#[must_use]` function", + None, "consider explicitly using function result" ) } diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 313a7e8569a..619dd1f54d6 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1402,6 +1402,7 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) { `if let` statement.", snippet(cx, arg.span, "_") ), + None, &format!( "consider replacing `for {0} in {1}` with `if let Some({0}) = {1}`", snippet(cx, pat.span, "_"), @@ -1418,6 +1419,7 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) { `if let` statement.", snippet(cx, arg.span, "_") ), + None, &format!( "consider replacing `for {0} in {1}` with `if let Ok({0}) = {1}`", snippet(cx, pat.span, "_"), diff --git a/clippy_lints/src/main_recursion.rs b/clippy_lints/src/main_recursion.rs index 7854873509e..8a0e47a3d31 100644 --- a/clippy_lints/src/main_recursion.rs +++ b/clippy_lints/src/main_recursion.rs @@ -53,6 +53,7 @@ impl LateLintPass<'_, '_> for MainRecursion { MAIN_RECURSION, func.span, &format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")), + None, "consider using another function for this recursion" ) } diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index a3a05fd1caa..27e31222d00 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -441,6 +441,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches { REST_PAT_IN_FULLY_BOUND_STRUCTS, pat.span, "unnecessary use of `..` pattern in struct binding. All fields were already bound", + None, "consider removing `..` from this binding", ); } @@ -887,6 +888,7 @@ fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, arms: &[Arm<'_>]) { WILDCARD_IN_OR_PATTERNS, arm.pat.span, "wildcard pattern covers any other pattern as it will match anyway.", + None, "Consider handling `_` separately.", ); } diff --git a/clippy_lints/src/mem_replace.rs b/clippy_lints/src/mem_replace.rs index 69639f45323..ab6865bf0f3 100644 --- a/clippy_lints/src/mem_replace.rs +++ b/clippy_lints/src/mem_replace.rs @@ -148,6 +148,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, expr_span MEM_REPLACE_WITH_UNINIT, expr_span, "replacing with `mem::uninitialized()`", + None, "consider using the `take_mut` crate instead", ); } else if cx.tcx.is_diagnostic_item(sym::mem_zeroed, repl_def_id) && @@ -157,6 +158,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, expr_span MEM_REPLACE_WITH_UNINIT, expr_span, "replacing with `mem::zeroed()`", + None, "consider using a default value or the `take_mut` crate instead", ); } diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 2337380c7dd..578ee9e0bfd 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2255,6 +2255,7 @@ fn lint_iter_nth<'a, 'tcx>( ITER_NTH, expr.span, &format!("called `.iter{0}().nth()` on a {1}", mut_str, caller_type), + None, &format!("calling `.get{}()` is both faster and more readable", mut_str), ); } @@ -2364,6 +2365,7 @@ fn lint_iter_skip_next(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) { ITER_SKIP_NEXT, expr.span, "called `skip(x).next()` on an iterator", + None, "this is more succinctly expressed by calling `nth(x)`", ); } @@ -2431,6 +2433,7 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi lint, expr.span, &format!("used `unwrap()` on `{}` value", kind,), + None, &format!( "if you don't want to handle the `{}` case gracefully, consider \ using `expect()` to provide a better panic message", @@ -2458,6 +2461,7 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi lint, expr.span, &format!("used `expect()` on `{}` value", kind,), + None, &format!("if this value is an `{}`, it will panic", none_value,), ); } @@ -2478,6 +2482,7 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir OK_EXPECT, expr.span, "called `ok().expect()` on a `Result` value", + None, "you can call `expect()` directly on the `Result`", ); } @@ -2774,6 +2779,7 @@ fn lint_skip_while_next<'a, 'tcx>( SKIP_WHILE_NEXT, expr.span, "called `skip_while(p).next()` on an `Iterator`", + None, "this is more succinctly expressed by calling `.find(!p)` instead", ); } @@ -2790,7 +2796,7 @@ fn lint_filter_map<'a, 'tcx>( if match_trait_method(cx, expr, &paths::ITERATOR) { let msg = "called `filter(p).map(q)` on an `Iterator`"; let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead"; - span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint); + span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint); } } @@ -2830,7 +2836,7 @@ fn lint_find_map<'a, 'tcx>( if match_trait_method(cx, &map_args[0], &paths::ITERATOR) { let msg = "called `find(p).map(q)` on an `Iterator`"; let hint = "this is more succinctly expressed by calling `.find_map(..)` instead"; - span_lint_and_help(cx, FIND_MAP, expr.span, msg, hint); + span_lint_and_help(cx, FIND_MAP, expr.span, msg, None, hint); } } @@ -2845,7 +2851,7 @@ fn lint_filter_map_map<'a, 'tcx>( if match_trait_method(cx, expr, &paths::ITERATOR) { let msg = "called `filter_map(p).map(q)` on an `Iterator`"; let hint = "this is more succinctly expressed by only calling `.filter_map(..)` instead"; - span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint); + span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint); } } @@ -2861,7 +2867,7 @@ fn lint_filter_flat_map<'a, 'tcx>( let msg = "called `filter(p).flat_map(q)` on an `Iterator`"; let hint = "this is more succinctly expressed by calling `.flat_map(..)` \ and filtering by returning `iter::empty()`"; - span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint); + span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint); } } @@ -2877,7 +2883,7 @@ fn lint_filter_map_flat_map<'a, 'tcx>( let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`"; let hint = "this is more succinctly expressed by calling `.flat_map(..)` \ and filtering by returning `iter::empty()`"; - span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint); + span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint); } } @@ -3260,6 +3266,7 @@ fn lint_suspicious_map(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) { SUSPICIOUS_MAP, expr.span, "this call to `map()` won't have an effect on the call to `count()`", + None, "make sure you did not confuse `map` with `filter` or `for_each`", ); } @@ -3640,7 +3647,7 @@ fn lint_filetype_is_file(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, args: & } let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb); let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary); - span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, &help_msg); + span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg); } fn fn_header_equals(expected: hir::FnHeader, actual: hir::FnHeader) -> bool { diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 75bbf0514c2..adfd8dfb1c1 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -313,6 +313,7 @@ impl EarlyLintPass for MiscEarlyLints { UNNEEDED_FIELD_PATTERN, pat.span, "All the struct fields are matched to a wildcard pattern, consider using `..`.", + None, &format!("Try with `{} {{ .. }}` instead", type_name), ); return; @@ -348,6 +349,7 @@ impl EarlyLintPass for MiscEarlyLints { field.span, "You matched a field with a wildcard pattern. Consider using `..` \ instead", + None, &format!("Try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")), ); } diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 6be4b1effea..28183810df4 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -304,6 +304,7 @@ fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, NEEDLESS_CONTINUE, expr.span, message, + None, &format!("{}\n{}", header, snip), ); } diff --git a/clippy_lints/src/option_env_unwrap.rs b/clippy_lints/src/option_env_unwrap.rs index 96ab4238008..66dfa20edb5 100644 --- a/clippy_lints/src/option_env_unwrap.rs +++ b/clippy_lints/src/option_env_unwrap.rs @@ -46,6 +46,7 @@ impl EarlyLintPass for OptionEnvUnwrap { OPTION_ENV_UNWRAP, expr.span, "this will panic at run-time if the environment variable doesn't exist at compile-time", + None, "consider using the `env!` macro instead" ); } diff --git a/clippy_lints/src/regex.rs b/clippy_lints/src/regex.rs index 4bcb9198792..30084e3e1ff 100644 --- a/clippy_lints/src/regex.rs +++ b/clippy_lints/src/regex.rs @@ -208,7 +208,7 @@ fn check_regex<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, utf8: match parser.parse(r) { Ok(r) => { if let Some(repl) = is_trivial_regex(&r) { - span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", repl); + span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl); } }, Err(regex_syntax::Error::Parse(e)) => { @@ -236,7 +236,7 @@ fn check_regex<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, utf8: match parser.parse(&r) { Ok(r) => { if let Some(repl) = is_trivial_regex(&r) { - span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", repl); + span_lint_and_help(cx, TRIVIAL_REGEX, expr.span, "trivial regex", None, repl); } }, Err(regex_syntax::Error::Parse(e)) => { diff --git a/clippy_lints/src/trait_bounds.rs b/clippy_lints/src/trait_bounds.rs index 075df19a71e..67121729663 100644 --- a/clippy_lints/src/trait_bounds.rs +++ b/clippy_lints/src/trait_bounds.rs @@ -76,6 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds { TYPE_REPETITION_IN_BOUNDS, p.span, "this type has already been used as a bound predicate", + None, &hint_string, ); } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 31d8daa2d97..e1d97973caa 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -343,6 +343,7 @@ impl Types { BOX_VEC, hir_ty.span, "you seem to be trying to use `Box>`. Consider using just `Vec`", + None, "`Vec` is already on the heap, `Box>` makes an extra allocation.", ); return; // don't recurse into the type @@ -437,6 +438,7 @@ impl Types { LINKEDLIST, hir_ty.span, "I see you're using a LinkedList! Perhaps you meant some other data structure?", + None, "a `VecDeque` might work", ); return; // don't recurse into the type @@ -1900,7 +1902,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AbsurdExtremeComparisons { conclusion ); - span_lint_and_help(cx, ABSURD_EXTREME_COMPARISONS, expr.span, msg, &help); + span_lint_and_help(cx, ABSURD_EXTREME_COMPARISONS, expr.span, msg, None, &help); } } } diff --git a/clippy_lints/src/unnamed_address.rs b/clippy_lints/src/unnamed_address.rs index b6473fc594e..4e077b95b5c 100644 --- a/clippy_lints/src/unnamed_address.rs +++ b/clippy_lints/src/unnamed_address.rs @@ -89,6 +89,7 @@ impl LateLintPass<'_, '_> for UnnamedAddress { VTABLE_ADDRESS_COMPARISONS, expr.span, "comparing trait object pointers compares a non-unique vtable address", + None, "consider extracting and comparing data pointers only", ); } @@ -109,6 +110,7 @@ impl LateLintPass<'_, '_> for UnnamedAddress { VTABLE_ADDRESS_COMPARISONS, expr.span, "comparing trait object pointers compares a non-unique vtable address", + None, "consider extracting and comparing data pointers only", ); } diff --git a/clippy_lints/src/unused_self.rs b/clippy_lints/src/unused_self.rs index 4483059e9ec..3d5e2f9fd21 100644 --- a/clippy_lints/src/unused_self.rs +++ b/clippy_lints/src/unused_self.rs @@ -69,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf { UNUSED_SELF, self_param.span, "unused `self` argument", + None, "consider refactoring to a associated function", ); return; diff --git a/clippy_lints/src/utils/diagnostics.rs b/clippy_lints/src/utils/diagnostics.rs index 3dff710bd85..3fb6cf966af 100644 --- a/clippy_lints/src/utils/diagnostics.rs +++ b/clippy_lints/src/utils/diagnostics.rs @@ -62,12 +62,23 @@ pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) { - cx.struct_span_lint(lint, span, |diag| { - let mut diag = diag.build(msg); - diag.help(help); - docs_link(&mut diag, lint); - diag.emit(); +pub fn span_lint_and_help<'a, T: LintContext>( + cx: &'a T, + lint: &'static Lint, + span: Span, + msg: &str, + help_span: Option, + help: &str, +) { + cx.struct_span_lint(lint, span, |ldb| { + let mut db = ldb.build(msg); + if let Some(help_span) = help_span { + db.span_help(help_span, help); + } else { + db.help(help); + } + docs_link(&mut db, lint); + db.emit(); }); } diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index a2101234318..803d3b2bc86 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -196,8 +196,8 @@ declare_clippy_lint! { /// sugg.to_string(), /// Applicability::MachineApplicable, /// ); - /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg); - /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg); + /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg); + /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg); /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); /// ``` @@ -403,6 +403,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions { COMPILER_LINT_FUNCTIONS, path.ident.span, "usage of a compiler lint function", + None, &format!("please use the Clippy variant of this function: `{}`", sugg), ); } @@ -481,7 +482,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls { }, "span_help" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { let help_snippet = snippet(cx, span_call_args[2].span, r#""...""#); - suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow()); + suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), true); }, "span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { let note_snippet = snippet(cx, span_call_args[2].span, r#""...""#); @@ -489,7 +490,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls { }, "help" => { let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#); - suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow()); + suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), false); } "note" => { let note_snippet = snippet(cx, span_call_args[1].span, r#""...""#); @@ -573,7 +574,19 @@ fn suggest_suggestion( ); } -fn suggest_help(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: &AndThenSnippets<'_>, help: &str) { +fn suggest_help( + cx: &LateContext<'_, '_>, + expr: &Expr<'_>, + and_then_snippets: &AndThenSnippets<'_>, + help: &str, + with_span: bool, +) { + let option_span = if with_span { + format!("Some({})", and_then_snippets.span) + } else { + "None".to_string() + }; + span_lint_and_sugg( cx, COLLAPSIBLE_SPAN_LINT_CALLS, @@ -581,8 +594,13 @@ fn suggest_help(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: &A "this call is collapsible", "collapse into", format!( - "span_lint_and_help({}, {}, {}, {}, {})", - and_then_snippets.cx, and_then_snippets.lint, and_then_snippets.span, and_then_snippets.msg, help + "span_lint_and_help({}, {}, {}, {}, {}, {})", + and_then_snippets.cx, + and_then_snippets.lint, + and_then_snippets.span, + and_then_snippets.msg, + &option_span, + help ), Applicability::MachineApplicable, ); diff --git a/clippy_lints/src/verbose_file_reads.rs b/clippy_lints/src/verbose_file_reads.rs index 55d7983249a..4d8d4438d88 100644 --- a/clippy_lints/src/verbose_file_reads.rs +++ b/clippy_lints/src/verbose_file_reads.rs @@ -40,6 +40,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VerboseFileReads { VERBOSE_FILE_READS, expr.span, "use of `File::read_to_end`", + None, "consider using `fs::read` instead", ); } else if is_file_read_to_string(cx, expr) { @@ -48,6 +49,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VerboseFileReads { VERBOSE_FILE_READS, expr.span, "use of `File::read_to_string`", + None, "consider using `fs::read_to_string` instead", ) } diff --git a/clippy_lints/src/zero_div_zero.rs b/clippy_lints/src/zero_div_zero.rs index afd10d9ed53..fb4700d8743 100644 --- a/clippy_lints/src/zero_div_zero.rs +++ b/clippy_lints/src/zero_div_zero.rs @@ -49,6 +49,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv { ZERO_DIVIDED_BY_ZERO, expr.span, "constant division of `0.0` with `0.0` will always result in NaN", + None, &format!( "Consider using `{}::NAN` if you would like a constant representing NaN", float_type, diff --git a/doc/adding_lints.md b/doc/adding_lints.md index a66d4e66add..94d6ccb316e 100644 --- a/doc/adding_lints.md +++ b/doc/adding_lints.md @@ -265,6 +265,7 @@ impl EarlyLintPass for FooFunctions { FOO_FUNCTIONS, span, "function named `foo`", + None, "consider using a more meaningful name" ); } @@ -296,6 +297,7 @@ impl EarlyLintPass for FooFunctions { FOO_FUNCTIONS, span, "function named `foo`", + None, "consider using a more meaningful name" ); } diff --git a/tests/ui/collapsible_span_lint_calls.fixed b/tests/ui/collapsible_span_lint_calls.fixed index 19ff69ec667..a22046d34a2 100644 --- a/tests/ui/collapsible_span_lint_calls.fixed +++ b/tests/ui/collapsible_span_lint_calls.fixed @@ -22,7 +22,15 @@ where } #[allow(unused_variables)] -fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {} +fn span_lint_and_help<'a, T: LintContext>( + cx: &'a T, + lint: &'static Lint, + span: Span, + msg: &str, + option_span: Option, + help: &str, +) { +} #[allow(unused_variables)] fn span_lint_and_note<'a, T: LintContext>( @@ -65,8 +73,8 @@ impl EarlyLintPass for Pass { let predicate = true; span_lint_and_sugg(cx, TEST_LINT, expr.span, lint_msg, help_msg, sugg.to_string(), Applicability::MachineApplicable); - span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg); - span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg); + span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg); + span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg); span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); diff --git a/tests/ui/collapsible_span_lint_calls.rs b/tests/ui/collapsible_span_lint_calls.rs index af263fcbea1..69b95255751 100644 --- a/tests/ui/collapsible_span_lint_calls.rs +++ b/tests/ui/collapsible_span_lint_calls.rs @@ -22,7 +22,15 @@ where } #[allow(unused_variables)] -fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {} +fn span_lint_and_help<'a, T: LintContext>( + cx: &'a T, + lint: &'static Lint, + span: Span, + msg: &str, + option_span: Option, + help: &str, +) { +} #[allow(unused_variables)] fn span_lint_and_note<'a, T: LintContext>( diff --git a/tests/ui/collapsible_span_lint_calls.stderr b/tests/ui/collapsible_span_lint_calls.stderr index fa40c82c983..e6d5c3efa22 100644 --- a/tests/ui/collapsible_span_lint_calls.stderr +++ b/tests/ui/collapsible_span_lint_calls.stderr @@ -1,5 +1,5 @@ error: this call is collapsible - --> $DIR/collapsible_span_lint_calls.rs:67:9 + --> $DIR/collapsible_span_lint_calls.rs:75:9 | LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | | db.span_suggestion(expr.span, help_msg, sugg.to_string(), Applicability::MachineApplicable); @@ -14,23 +14,23 @@ LL | #![deny(clippy::internal)] = note: `#[deny(clippy::collapsible_span_lint_calls)]` implied by `#[deny(clippy::internal)]` error: this call is collapsible - --> $DIR/collapsible_span_lint_calls.rs:70:9 + --> $DIR/collapsible_span_lint_calls.rs:78:9 | LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | | db.span_help(expr.span, help_msg); LL | | }); - | |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg)` + | |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg)` error: this call is collapsible - --> $DIR/collapsible_span_lint_calls.rs:73:9 + --> $DIR/collapsible_span_lint_calls.rs:81:9 | LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | | db.help(help_msg); LL | | }); - | |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, help_msg)` + | |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg)` error: this call is collspible - --> $DIR/collapsible_span_lint_calls.rs:76:9 + --> $DIR/collapsible_span_lint_calls.rs:84:9 | LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | | db.span_note(expr.span, note_msg); @@ -38,7 +38,7 @@ LL | | }); | |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg)` error: this call is collspible - --> $DIR/collapsible_span_lint_calls.rs:79:9 + --> $DIR/collapsible_span_lint_calls.rs:87:9 | LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | | db.note(note_msg);