Skip to content

Commit 3d366fc

Browse files
committed
Auto merge of rust-lang#9026 - hellow554:neg_multiply_precedence, r=xFrednet
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
2 parents f9fea17 + 6fc84d4 commit 3d366fc

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

clippy_lints/src/neg_multiply.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::consts::{self, Constant};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::sugg::has_enclosing_paren;
45
use if_chain::if_chain;
6+
use rustc_ast::util::parser::PREC_PREFIX;
57
use rustc_errors::Applicability;
68
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
79
use rustc_lint::{LateContext, LateLintPass};
@@ -58,7 +60,12 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
5860

5961
then {
6062
let mut applicability = Applicability::MachineApplicable;
61-
let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability));
63+
let snip = snippet_with_applicability(cx, exp.span, "..", &mut applicability);
64+
let suggestion = if exp.precedence().order() < PREC_PREFIX && !has_enclosing_paren(&snip) {
65+
format!("-({})", snip)
66+
} else {
67+
format!("-{}", snip)
68+
};
6269
span_lint_and_sugg(
6370
cx,
6471
NEG_MULTIPLY,

tests/ui/neg_multiply.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ fn main() {
3838

3939
0xcafe | -0xff00;
4040

41+
-(3_usize as i32);
42+
-(3_usize as i32);
43+
4144
-1 * -1; // should be ok
4245

4346
X * -1; // should be ok

tests/ui/neg_multiply.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ fn main() {
3838

3939
0xcafe | 0xff00 * -1;
4040

41+
3_usize as i32 * -1;
42+
(3_usize as i32) * -1;
43+
4144
-1 * -1; // should be ok
4245

4346
X * -1; // should be ok

tests/ui/neg_multiply.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,17 @@ error: this multiplication by -1 can be written more succinctly
3636
LL | 0xcafe | 0xff00 * -1;
3737
| ^^^^^^^^^^^ help: consider using: `-0xff00`
3838

39-
error: aborting due to 6 previous errors
39+
error: this multiplication by -1 can be written more succinctly
40+
--> $DIR/neg_multiply.rs:41:5
41+
|
42+
LL | 3_usize as i32 * -1;
43+
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)`
44+
45+
error: this multiplication by -1 can be written more succinctly
46+
--> $DIR/neg_multiply.rs:42:5
47+
|
48+
LL | (3_usize as i32) * -1;
49+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)`
50+
51+
error: aborting due to 8 previous errors
4052

0 commit comments

Comments
 (0)