Skip to content

Commit

Permalink
Auto merge of #5900 - ThibsG:ParensUselessConversion4750, r=phansch
Browse files Browse the repository at this point in the history
Fix: keep parenthesis for suggestion in `useless_conversion` lint

Note: this lint was previously named `identity_conversion`.

fixes: #4750

changelog: fix parenthesis for `useless_conversion` lint suggestion
  • Loading branch information
bors committed Aug 13, 2020
2 parents dc1e09b + 5634c8d commit fc4fd91
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::sugg::Sugg;
use crate::utils::{
get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
Expand Down Expand Up @@ -158,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
if TyS::same_type(a, b);

then {
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
let sugg = Sugg::hir_with_macro_callsite(cx, &args[0], "<expr>").maybe_par();
let sugg_msg =
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
span_lint_and_sugg(
Expand All @@ -167,7 +168,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
e.span,
"useless conversion to the same type",
&sugg_msg,
sugg,
sugg.to_string(),
Applicability::MachineApplicable, // snippet
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/useless_conversion.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ fn main() {
let _ = "".lines();
let _ = vec![1, 2, 3].into_iter();
let _: String = format!("Hello {}", "world");

// keep parenthesis around `a + b` for suggestion (see #4750)
let a: i32 = 1;
let b: i32 = 1;
let _ = (a + b) * 3;
}
5 changes: 5 additions & 0 deletions tests/ui/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ fn main() {
let _ = "".lines().into_iter();
let _ = vec![1, 2, 3].into_iter().into_iter();
let _: String = format!("Hello {}", "world").into();

// keep parenthesis around `a + b` for suggestion (see #4750)
let a: i32 = 1;
let b: i32 = 1;
let _ = i32::from(a + b) * 3;
}
8 changes: 7 additions & 1 deletion tests/ui/useless_conversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ error: useless conversion to the same type
LL | let _: String = format!("Hello {}", "world").into();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`

error: aborting due to 10 previous errors
error: useless conversion to the same type
--> $DIR/useless_conversion.rs:71:13
|
LL | let _ = i32::from(a + b) * 3;
| ^^^^^^^^^^^^^^^^ help: consider removing `i32::from()`: `(a + b)`

error: aborting due to 11 previous errors

0 comments on commit fc4fd91

Please sign in to comment.