Skip to content

Commit

Permalink
Auto merge of rust-lang#9026 - hellow554:neg_multiply_precedence, r=x…
Browse files Browse the repository at this point in the history
…Frednet

put parentheses around neg_multiply suggestion if needed

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`neg_multiply`]: put parentheses around suggestion if needed
  • Loading branch information
bors committed Jun 22, 2022
2 parents f9fea17 + 6fc84d4 commit 3d366fc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion clippy_lints/src/neg_multiply.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/neg_multiply.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/neg_multiply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/neg_multiply.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3d366fc

Please sign in to comment.