diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index acc83d28897..588f5f75c1e 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -1,5 +1,6 @@ use crate::utils::{ - constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, + constants, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, + span_lint_and_then, }; use if_chain::if_chain; use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass}; @@ -414,13 +415,10 @@ impl EarlyLintPass for MiscEarlyLints { "Try not to call a closure in the expression where it is declared.", |db| { if decl.inputs.is_empty() { - let hint = snippet(cx, block.span, "..").into_owned(); - db.span_suggestion( - expr.span, - "Try doing something like: ", - hint, - Applicability::MachineApplicable, // snippet - ); + let mut app = Applicability::MachineApplicable; + let hint = + snippet_with_applicability(cx, block.span, "..", &mut app).into_owned(); + db.span_suggestion(expr.span, "Try doing something like: ", hint, app); } }, ); diff --git a/tests/ui/redundant_closure_call.rs b/tests/ui/redundant_closure_call.rs index 2e81eea4400..bacd67db7c3 100644 --- a/tests/ui/redundant_closure_call.rs +++ b/tests/ui/redundant_closure_call.rs @@ -1,8 +1,8 @@ +// non rustfixable, see redundant_closure_call_fixable.rs + #![warn(clippy::redundant_closure_call)] fn main() { - let a = (|| 42)(); - let mut i = 1; let mut k = (|m| m + 1)(i); diff --git a/tests/ui/redundant_closure_call.stderr b/tests/ui/redundant_closure_call.stderr index 9c827fd8f17..68c1416bb6b 100644 --- a/tests/ui/redundant_closure_call.stderr +++ b/tests/ui/redundant_closure_call.stderr @@ -12,12 +12,6 @@ error: Closure called just once immediately after it was declared LL | i = closure(3); | ^^^^^^^^^^^^^^ -error: Try not to call a closure in the expression where it is declared. - --> $DIR/redundant_closure_call.rs:4:13 - | -LL | let a = (|| 42)(); - | ^^^^^^^^^ help: Try doing something like: : `42` - error: Try not to call a closure in the expression where it is declared. --> $DIR/redundant_closure_call.rs:7:17 | @@ -30,5 +24,5 @@ error: Try not to call a closure in the expression where it is declared. LL | k = (|a, b| a * b)(1, 5); | ^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors diff --git a/tests/ui/redundant_closure_call_fixable.fixed b/tests/ui/redundant_closure_call_fixable.fixed new file mode 100644 index 00000000000..0abca6fca06 --- /dev/null +++ b/tests/ui/redundant_closure_call_fixable.fixed @@ -0,0 +1,8 @@ +// run-rustfix + +#![warn(clippy::redundant_closure_call)] +#![allow(unused)] + +fn main() { + let a = 42; +} diff --git a/tests/ui/redundant_closure_call_fixable.rs b/tests/ui/redundant_closure_call_fixable.rs new file mode 100644 index 00000000000..f8b9d37a5cc --- /dev/null +++ b/tests/ui/redundant_closure_call_fixable.rs @@ -0,0 +1,8 @@ +// run-rustfix + +#![warn(clippy::redundant_closure_call)] +#![allow(unused)] + +fn main() { + let a = (|| 42)(); +} diff --git a/tests/ui/redundant_closure_call_fixable.stderr b/tests/ui/redundant_closure_call_fixable.stderr new file mode 100644 index 00000000000..e7737f9dd85 --- /dev/null +++ b/tests/ui/redundant_closure_call_fixable.stderr @@ -0,0 +1,10 @@ +error: Try not to call a closure in the expression where it is declared. + --> $DIR/redundant_closure_call_fixable.rs:7:13 + | +LL | let a = (|| 42)(); + | ^^^^^^^^^ help: Try doing something like: : `42` + | + = note: `-D clippy::redundant-closure-call` implied by `-D warnings` + +error: aborting due to previous error +