diff options
| author | Marcel Hellwig <git@cookiesoft.de> | 2022-06-20 23:44:18 +0200 |
|---|---|---|
| committer | Marcel Hellwig <git@cookiesoft.de> | 2022-06-20 23:47:53 +0200 |
| commit | 6fc84d4da39942857e243cfe951bbefbdba5250b (patch) | |
| tree | 50746d19f27ce2b02b05e8be86c3ac9f1e2d2e80 | |
| parent | 93c6f9ebed65eb4d77a5cf1ccf670cef3b1fca9e (diff) | |
| download | rust-6fc84d4da39942857e243cfe951bbefbdba5250b.tar.gz rust-6fc84d4da39942857e243cfe951bbefbdba5250b.zip | |
put parentheses around neg_multiply suggestion if needed
| -rw-r--r-- | clippy_lints/src/neg_multiply.rs | 9 | ||||
| -rw-r--r-- | tests/ui/neg_multiply.fixed | 3 | ||||
| -rw-r--r-- | tests/ui/neg_multiply.rs | 3 | ||||
| -rw-r--r-- | tests/ui/neg_multiply.stderr | 14 |
4 files changed, 27 insertions, 2 deletions
diff --git a/clippy_lints/src/neg_multiply.rs b/clippy_lints/src/neg_multiply.rs index ce6bb38b7c0..b087cfb36b1 100644 --- a/clippy_lints/src/neg_multiply.rs +++ b/clippy_lints/src/neg_multiply.rs @@ -1,7 +1,9 @@ use clippy_utils::consts::{self, Constant}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; +use clippy_utils::sugg::has_enclosing_paren; use if_chain::if_chain; +use rustc_ast::util::parser::PREC_PREFIX; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; @@ -58,7 +60,12 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) { then { let mut applicability = Applicability::MachineApplicable; - let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability)); + let snip = snippet_with_applicability(cx, exp.span, "..", &mut applicability); + let suggestion = if exp.precedence().order() < PREC_PREFIX && !has_enclosing_paren(&snip) { + format!("-({})", snip) + } else { + format!("-{}", snip) + }; span_lint_and_sugg( cx, NEG_MULTIPLY, diff --git a/tests/ui/neg_multiply.fixed b/tests/ui/neg_multiply.fixed index 35af9d6ae31..58ab9e85678 100644 --- a/tests/ui/neg_multiply.fixed +++ b/tests/ui/neg_multiply.fixed @@ -38,6 +38,9 @@ fn main() { 0xcafe | -0xff00; + -(3_usize as i32); + -(3_usize as i32); + -1 * -1; // should be ok X * -1; // should be ok diff --git a/tests/ui/neg_multiply.rs b/tests/ui/neg_multiply.rs index 7dbdb0906ce..581290dc72e 100644 --- a/tests/ui/neg_multiply.rs +++ b/tests/ui/neg_multiply.rs @@ -38,6 +38,9 @@ fn main() { 0xcafe | 0xff00 * -1; + 3_usize as i32 * -1; + (3_usize as i32) * -1; + -1 * -1; // should be ok X * -1; // should be ok diff --git a/tests/ui/neg_multiply.stderr b/tests/ui/neg_multiply.stderr index dbf8fb36938..388ef29eb85 100644 --- a/tests/ui/neg_multiply.stderr +++ b/tests/ui/neg_multiply.stderr @@ -36,5 +36,17 @@ error: this multiplication by -1 can be written more succinctly LL | 0xcafe | 0xff00 * -1; | ^^^^^^^^^^^ help: consider using: `-0xff00` -error: aborting due to 6 previous errors +error: this multiplication by -1 can be written more succinctly + --> $DIR/neg_multiply.rs:41:5 + | +LL | 3_usize as i32 * -1; + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)` + +error: this multiplication by -1 can be written more succinctly + --> $DIR/neg_multiply.rs:42:5 + | +LL | (3_usize as i32) * -1; + | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)` + +error: aborting due to 8 previous errors |
