]> git.lizzy.rs Git - rust.git/commitdiff
refactor: use clippy_utils::Sugg instead of direct string ops
authorJacob Kiesel <kieseljake@live.com>
Wed, 14 Sep 2022 19:28:54 +0000 (13:28 -0600)
committerJacob Kiesel <kieseljake@live.com>
Wed, 14 Sep 2022 19:35:35 +0000 (13:35 -0600)
clippy_lints/src/bool_to_int_with_if.rs
tests/ui/bool_to_int_with_if.fixed
tests/ui/bool_to_int_with_if.stderr

index bea74279e0335f36b8bab121292ab5d3f886e1de..51e98cda845193958d907b4fc721587dda8118a7 100644 (file)
@@ -1,9 +1,9 @@
-use rustc_ast::{ExprPrecedence, LitKind};
+use rustc_ast::LitKind;
 use rustc_hir::{Block, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
-use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, source::snippet_block_with_applicability};
+use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, sugg::Sugg};
 use rustc_errors::Applicability;
 
 declare_clippy_lint! {
@@ -69,28 +69,27 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
             return;
         };
         let mut applicability = Applicability::MachineApplicable;
-        let snippet = snippet_block_with_applicability(ctx, check.span, "..", None, &mut applicability);
-
-        let invert = if inverted { "!" } else { "" };
-        let need_parens = should_have_parentheses(check);
-
-        let snippet_with_braces = {
-            let (left_paren, right_paren) = if need_parens {("(", ")")} else {("", "")};
-            format!("{invert}{left_paren}{snippet}{right_paren}")
+        let snippet = {
+            let mut sugg = Sugg::hir_with_applicability(ctx, check, "..", &mut applicability);
+            if inverted {
+                sugg = !sugg;
+            }
+            sugg
         };
 
         let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type
 
         let suggestion = {
             let wrap_in_curly = is_else_clause(ctx.tcx, expr);
-            let (left_curly, right_curly) = if wrap_in_curly {("{", "}")} else {("", "")};
-            let (left_paren, right_paren) = if inverted && need_parens {("(", ")")} else {("", "")};
-            format!(
-                "{left_curly}{ty}::from({invert}{left_paren}{snippet}{right_paren}){right_curly}"
-            )
+            let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into());
+            if wrap_in_curly {
+                s = s.blockify();
+            }
+            s
         }; // when used in else clause if statement should be wrapped in curly braces
 
-        let (inverted_left_paren, inverted_right_paren) = if inverted {("(", ")")} else {("", "")};
+        let into_snippet = snippet.clone().maybe_par();
+        let as_snippet = snippet.as_ty(ty);
 
         span_lint_and_then(ctx,
             BOOL_TO_INT_WITH_IF,
@@ -103,7 +102,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
                 suggestion,
                 applicability,
             );
-            diag.note(format!("`{snippet_with_braces} as {ty}` or `{inverted_left_paren}{snippet_with_braces}{inverted_right_paren}.into()` can also be valid options"));
+            diag.note(format!("`{as_snippet}` or `{into_snippet}.into()` can also be valid options"));
         });
     };
 }
@@ -135,7 +134,3 @@ fn check_int_literal_equals_val<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>, expecte
         false
     }
 }
-
-fn should_have_parentheses<'tcx>(check: &'tcx rustc_hir::Expr<'tcx>) -> bool {
-    check.precedence().order() < ExprPrecedence::Cast.order()
-}
index c48dc941b84b989d4149ad54a82307b87b63800b..2c8339cdd7f8a0fa97d0c3c495fc9d90cfb611f3 100644 (file)
@@ -22,12 +22,12 @@ fn main() {
     // if else if
     if a {
         123
-    } else {i32::from(b)};
+    } else { i32::from(b) };
 
     // if else if inverted
     if a {
         123
-    } else {i32::from(!b)};
+    } else { i32::from(!b) };
 
     // Shouldn't lint
 
index cc3e0395aa4ffc04fd9e41deb5ecd3e7eee2b011..e695440f6682ff3dd68792a60e6c905cf0f51c3f 100644 (file)
@@ -33,7 +33,7 @@ LL | |         0
 LL | |     };
    | |_____^ help: replace with from: `i32::from(!a)`
    |
-   = note: `!a as i32` or `!a.into()` can also be valid options
+   = note: `!a as i32` or `(!a).into()` can also be valid options
 
 error: boolean to int conversion using if
   --> $DIR/bool_to_int_with_if.rs:30:5
@@ -80,7 +80,7 @@ LL | |         1
 LL | |     } else {
 LL | |         0
 LL | |     };
-   | |_____^ help: replace with from: `{i32::from(b)}`
+   | |_____^ help: replace with from: `{ i32::from(b) }`
    |
    = note: `b as i32` or `b.into()` can also be valid options
 
@@ -93,7 +93,7 @@ LL | |         0
 LL | |     } else {
 LL | |         1
 LL | |     };
-   | |_____^ help: replace with from: `{i32::from(!b)}`
+   | |_____^ help: replace with from: `{ i32::from(!b) }`
    |
    = note: `!b as i32` or `(!b).into()` can also be valid options