Skip to content

Commit fc4fd91

Browse files
committed
Auto merge of #5900 - ThibsG:ParensUselessConversion4750, r=phansch
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
2 parents dc1e09b + 5634c8d commit fc4fd91

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clippy_lints/src/useless_conversion.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::sugg::Sugg;
12
use crate::utils::{
23
get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
34
snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
@@ -158,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
158159
if TyS::same_type(a, b);
159160

160161
then {
161-
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
162+
let sugg = Sugg::hir_with_macro_callsite(cx, &args[0], "<expr>").maybe_par();
162163
let sugg_msg =
163164
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
164165
span_lint_and_sugg(
@@ -167,7 +168,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
167168
e.span,
168169
"useless conversion to the same type",
169170
&sugg_msg,
170-
sugg,
171+
sugg.to_string(),
171172
Applicability::MachineApplicable, // snippet
172173
);
173174
}

tests/ui/useless_conversion.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ fn main() {
6464
let _ = "".lines();
6565
let _ = vec![1, 2, 3].into_iter();
6666
let _: String = format!("Hello {}", "world");
67+
68+
// keep parenthesis around `a + b` for suggestion (see #4750)
69+
let a: i32 = 1;
70+
let b: i32 = 1;
71+
let _ = (a + b) * 3;
6772
}

tests/ui/useless_conversion.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ fn main() {
6464
let _ = "".lines().into_iter();
6565
let _ = vec![1, 2, 3].into_iter().into_iter();
6666
let _: String = format!("Hello {}", "world").into();
67+
68+
// keep parenthesis around `a + b` for suggestion (see #4750)
69+
let a: i32 = 1;
70+
let b: i32 = 1;
71+
let _ = i32::from(a + b) * 3;
6772
}

tests/ui/useless_conversion.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,11 @@ error: useless conversion to the same type
6464
LL | let _: String = format!("Hello {}", "world").into();
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`
6666

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

0 commit comments

Comments
 (0)