about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibsG <Thibs@debian.com>2020-08-12 21:37:27 +0200
committerThibsG <Thibs@debian.com>2020-08-12 21:44:02 +0200
commit5634c8da02862653be557c6ab1242a6c9ce86ce8 (patch)
tree01a0b8ed405d58204d9d27f6eacca33f8a8420c7
parent3ed0a4ce5dd199c2abb1241fd59a5bdfb3bc5ee8 (diff)
downloadrust-5634c8da02862653be557c6ab1242a6c9ce86ce8.tar.gz
rust-5634c8da02862653be557c6ab1242a6c9ce86ce8.zip
Fix: keep parenthesis for suggestion in `useless_conversion` lint
-rw-r--r--clippy_lints/src/useless_conversion.rs5
-rw-r--r--tests/ui/useless_conversion.fixed5
-rw-r--r--tests/ui/useless_conversion.rs5
-rw-r--r--tests/ui/useless_conversion.stderr8
4 files changed, 20 insertions, 3 deletions
diff --git a/clippy_lints/src/useless_conversion.rs b/clippy_lints/src/useless_conversion.rs
index 1bf37632e32..4ab2b5e796d 100644
--- a/clippy_lints/src/useless_conversion.rs
+++ b/clippy_lints/src/useless_conversion.rs
@@ -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,
@@ -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(
@@ -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
                                 );
                             }
diff --git a/tests/ui/useless_conversion.fixed b/tests/ui/useless_conversion.fixed
index 813cdaecaa9..8a9b0cd3cf0 100644
--- a/tests/ui/useless_conversion.fixed
+++ b/tests/ui/useless_conversion.fixed
@@ -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;
 }
diff --git a/tests/ui/useless_conversion.rs b/tests/ui/useless_conversion.rs
index 540fea23b36..4faa1572973 100644
--- a/tests/ui/useless_conversion.rs
+++ b/tests/ui/useless_conversion.rs
@@ -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;
 }
diff --git a/tests/ui/useless_conversion.stderr b/tests/ui/useless_conversion.stderr
index b958b035452..f1e880d2696 100644
--- a/tests/ui/useless_conversion.stderr
+++ b/tests/ui/useless_conversion.stderr
@@ -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