Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: keep parenthesis for suggestion in useless_conversion lint #5900

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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