Skip to content

Commit de560c3

Browse files
committed
Implement ambiguous_negative_literals lint
1 parent 0c81f94 commit de560c3

File tree

6 files changed

+321
-0
lines changed

6 files changed

+321
-0
lines changed

compiler/rustc_lint/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ lint_ambiguous_glob_reexport = ambiguous glob re-exports
55
.label_first_reexport = the name `{$name}` in the {$namespace} namespace is first re-exported here
66
.label_duplicate_reexport = but the name `{$name}` in the {$namespace} namespace is also re-exported here
77
8+
lint_ambiguous_negative_literals = `-` has lower precedence than method calls, which might be unexpected
9+
.example = e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
10+
.negative_literal = add parentheses around the `-` and the literal to call the method on a negative literal
11+
.current_behavior = add parentheses around the literal and the method call to keep the current behavior
12+
813
lint_ambiguous_wide_pointer_comparisons = ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
914
.addr_metadata_suggestion = use explicit `std::ptr::eq` method to compare metadata and addresses
1015
.addr_suggestion = use `std::ptr::addr_eq` or untyped pointers to only compare their addresses

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ mod noop_method_call;
7272
mod opaque_hidden_inferred_bound;
7373
mod pass_by_value;
7474
mod passes;
75+
mod precedence;
7576
mod ptr_nulls;
7677
mod redundant_semicolon;
7778
mod reference_casting;
@@ -109,6 +110,7 @@ use nonstandard_style::*;
109110
use noop_method_call::*;
110111
use opaque_hidden_inferred_bound::*;
111112
use pass_by_value::*;
113+
use precedence::*;
112114
use ptr_nulls::*;
113115
use redundant_semicolon::*;
114116
use reference_casting::*;
@@ -173,6 +175,7 @@ early_lint_methods!(
173175
RedundantSemicolons: RedundantSemicolons,
174176
UnusedDocComment: UnusedDocComment,
175177
Expr2024: Expr2024,
178+
Precedence: Precedence,
176179
]
177180
]
178181
);

compiler/rustc_lint/src/lints.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,35 @@ pub struct NonLocalDefinitionsCargoUpdateNote {
14891489
pub crate_name: Symbol,
14901490
}
14911491

1492+
// precedence.rs
1493+
#[derive(LintDiagnostic)]
1494+
#[diag(lint_ambiguous_negative_literals)]
1495+
#[note(lint_example)]
1496+
pub struct AmbiguousNegativeLiteralsDiag {
1497+
#[subdiagnostic]
1498+
pub negative_literal: AmbiguousNegativeLiteralsNegativeLiteralSuggestion,
1499+
#[subdiagnostic]
1500+
pub current_behavior: AmbiguousNegativeLiteralsCurrentBehaviorSuggestion,
1501+
}
1502+
1503+
#[derive(Subdiagnostic)]
1504+
#[multipart_suggestion(lint_negative_literal, applicability = "maybe-incorrect")]
1505+
pub struct AmbiguousNegativeLiteralsNegativeLiteralSuggestion {
1506+
#[suggestion_part(code = "(")]
1507+
pub start_span: Span,
1508+
#[suggestion_part(code = ")")]
1509+
pub end_span: Span,
1510+
}
1511+
1512+
#[derive(Subdiagnostic)]
1513+
#[multipart_suggestion(lint_current_behavior, applicability = "maybe-incorrect")]
1514+
pub struct AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
1515+
#[suggestion_part(code = "(")]
1516+
pub start_span: Span,
1517+
#[suggestion_part(code = ")")]
1518+
pub end_span: Span,
1519+
}
1520+
14921521
// pass_by_value.rs
14931522
#[derive(LintDiagnostic)]
14941523
#[diag(lint_pass_by_value)]

compiler/rustc_lint/src/precedence.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use rustc_ast::token::LitKind;
2+
use rustc_ast::{Expr, ExprKind, MethodCall, UnOp};
3+
use rustc_session::{declare_lint, declare_lint_pass};
4+
5+
use crate::lints::{
6+
AmbiguousNegativeLiteralsCurrentBehaviorSuggestion, AmbiguousNegativeLiteralsDiag,
7+
AmbiguousNegativeLiteralsNegativeLiteralSuggestion,
8+
};
9+
use crate::{EarlyContext, EarlyLintPass, LintContext};
10+
11+
declare_lint! {
12+
/// The `ambiguous_negative_literals` lint checks for cases that are
13+
/// confusing between a negative literal and a negation that's not part
14+
/// of the literal.
15+
///
16+
/// ### Example
17+
///
18+
/// ```rust,compile_fail
19+
/// # #![allow(unused)]
20+
/// -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1
21+
/// ```
22+
///
23+
/// {{produces}}
24+
///
25+
/// ### Explanation
26+
///
27+
/// Method calls take precedence over unary precedence. Setting the
28+
/// precedence explicitly makes the code clearer and avoid potential bugs.
29+
pub AMBIGUOUS_NEGATIVE_LITERALS,
30+
Deny,
31+
"ambiguous negative literals operations",
32+
report_in_external_macro
33+
}
34+
35+
declare_lint_pass!(Precedence => [AMBIGUOUS_NEGATIVE_LITERALS]);
36+
37+
impl EarlyLintPass for Precedence {
38+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
39+
let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind else {
40+
return;
41+
};
42+
43+
let mut arg = operand;
44+
let mut at_least_one = false;
45+
while let ExprKind::MethodCall(box MethodCall { receiver, .. }) = &arg.kind {
46+
at_least_one = true;
47+
arg = receiver;
48+
}
49+
50+
if at_least_one
51+
&& let ExprKind::Lit(lit) = &arg.kind
52+
&& let LitKind::Integer | LitKind::Float = &lit.kind
53+
{
54+
cx.emit_span_lint(
55+
AMBIGUOUS_NEGATIVE_LITERALS,
56+
expr.span,
57+
AmbiguousNegativeLiteralsDiag {
58+
negative_literal: AmbiguousNegativeLiteralsNegativeLiteralSuggestion {
59+
start_span: expr.span.shrink_to_lo(),
60+
end_span: arg.span.shrink_to_hi(),
61+
},
62+
current_behavior: AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
63+
start_span: operand.span.shrink_to_lo(),
64+
end_span: operand.span.shrink_to_hi(),
65+
},
66+
},
67+
);
68+
}
69+
}
70+
}

tests/ui/lint/negative_literals.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@ check-fail
2+
3+
fn main() {
4+
let _ = -1i32.abs();
5+
//~^ ERROR `-` has lower precedence than method calls
6+
let _ = -1f32.abs();
7+
//~^ ERROR `-` has lower precedence than method calls
8+
let _ = -1f64.asin();
9+
//~^ ERROR `-` has lower precedence than method calls
10+
let _ = -1f64.asinh();
11+
//~^ ERROR `-` has lower precedence than method calls
12+
let _ = -1f64.tan();
13+
//~^ ERROR `-` has lower precedence than method calls
14+
let _ = -1f64.tanh();
15+
//~^ ERROR `-` has lower precedence than method calls
16+
let _ = -1.0_f64.cos().cos();
17+
//~^ ERROR `-` has lower precedence than method calls
18+
let _ = -1.0_f64.cos().sin();
19+
//~^ ERROR `-` has lower precedence than method calls
20+
let _ = -1.0_f64.sin().cos();
21+
//~^ ERROR `-` has lower precedence than method calls
22+
let _ = -1f64.sin().sin();
23+
//~^ ERROR `-` has lower precedence than method calls
24+
25+
dbg!( -1.0_f32.cos() );
26+
//~^ ERROR `-` has lower precedence than method calls
27+
28+
// should not warn
29+
let _ = (-1i32).abs();
30+
let _ = (-1f32).abs();
31+
let _ = -(1i32).abs();
32+
let _ = -(1f32).abs();
33+
let _ = -(1i32.abs());
34+
let _ = -(1f32.abs());
35+
}
+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
error: `-` has lower precedence than method calls, which might be unexpected
2+
--> $DIR/negative_literals.rs:4:13
3+
|
4+
LL | let _ = -1i32.abs();
5+
| ^^^^^^^^^^^
6+
|
7+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
8+
= note: `#[deny(ambiguous_negative_literals)]` on by default
9+
help: add parentheses around the `-` and the literal to call the method on a negative literal
10+
|
11+
LL | let _ = (-1i32).abs();
12+
| + +
13+
help: add parentheses around the literal and the method call to keep the current behavior
14+
|
15+
LL | let _ = -(1i32.abs());
16+
| + +
17+
18+
error: `-` has lower precedence than method calls, which might be unexpected
19+
--> $DIR/negative_literals.rs:6:13
20+
|
21+
LL | let _ = -1f32.abs();
22+
| ^^^^^^^^^^^
23+
|
24+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
25+
help: add parentheses around the `-` and the literal to call the method on a negative literal
26+
|
27+
LL | let _ = (-1f32).abs();
28+
| + +
29+
help: add parentheses around the literal and the method call to keep the current behavior
30+
|
31+
LL | let _ = -(1f32.abs());
32+
| + +
33+
34+
error: `-` has lower precedence than method calls, which might be unexpected
35+
--> $DIR/negative_literals.rs:8:13
36+
|
37+
LL | let _ = -1f64.asin();
38+
| ^^^^^^^^^^^^
39+
|
40+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
41+
help: add parentheses around the `-` and the literal to call the method on a negative literal
42+
|
43+
LL | let _ = (-1f64).asin();
44+
| + +
45+
help: add parentheses around the literal and the method call to keep the current behavior
46+
|
47+
LL | let _ = -(1f64.asin());
48+
| + +
49+
50+
error: `-` has lower precedence than method calls, which might be unexpected
51+
--> $DIR/negative_literals.rs:10:13
52+
|
53+
LL | let _ = -1f64.asinh();
54+
| ^^^^^^^^^^^^^
55+
|
56+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
57+
help: add parentheses around the `-` and the literal to call the method on a negative literal
58+
|
59+
LL | let _ = (-1f64).asinh();
60+
| + +
61+
help: add parentheses around the literal and the method call to keep the current behavior
62+
|
63+
LL | let _ = -(1f64.asinh());
64+
| + +
65+
66+
error: `-` has lower precedence than method calls, which might be unexpected
67+
--> $DIR/negative_literals.rs:12:13
68+
|
69+
LL | let _ = -1f64.tan();
70+
| ^^^^^^^^^^^
71+
|
72+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
73+
help: add parentheses around the `-` and the literal to call the method on a negative literal
74+
|
75+
LL | let _ = (-1f64).tan();
76+
| + +
77+
help: add parentheses around the literal and the method call to keep the current behavior
78+
|
79+
LL | let _ = -(1f64.tan());
80+
| + +
81+
82+
error: `-` has lower precedence than method calls, which might be unexpected
83+
--> $DIR/negative_literals.rs:14:13
84+
|
85+
LL | let _ = -1f64.tanh();
86+
| ^^^^^^^^^^^^
87+
|
88+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
89+
help: add parentheses around the `-` and the literal to call the method on a negative literal
90+
|
91+
LL | let _ = (-1f64).tanh();
92+
| + +
93+
help: add parentheses around the literal and the method call to keep the current behavior
94+
|
95+
LL | let _ = -(1f64.tanh());
96+
| + +
97+
98+
error: `-` has lower precedence than method calls, which might be unexpected
99+
--> $DIR/negative_literals.rs:16:13
100+
|
101+
LL | let _ = -1.0_f64.cos().cos();
102+
| ^^^^^^^^^^^^^^^^^^^^
103+
|
104+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
105+
help: add parentheses around the `-` and the literal to call the method on a negative literal
106+
|
107+
LL | let _ = (-1.0_f64).cos().cos();
108+
| + +
109+
help: add parentheses around the literal and the method call to keep the current behavior
110+
|
111+
LL | let _ = -(1.0_f64.cos().cos());
112+
| + +
113+
114+
error: `-` has lower precedence than method calls, which might be unexpected
115+
--> $DIR/negative_literals.rs:18:13
116+
|
117+
LL | let _ = -1.0_f64.cos().sin();
118+
| ^^^^^^^^^^^^^^^^^^^^
119+
|
120+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
121+
help: add parentheses around the `-` and the literal to call the method on a negative literal
122+
|
123+
LL | let _ = (-1.0_f64).cos().sin();
124+
| + +
125+
help: add parentheses around the literal and the method call to keep the current behavior
126+
|
127+
LL | let _ = -(1.0_f64.cos().sin());
128+
| + +
129+
130+
error: `-` has lower precedence than method calls, which might be unexpected
131+
--> $DIR/negative_literals.rs:20:13
132+
|
133+
LL | let _ = -1.0_f64.sin().cos();
134+
| ^^^^^^^^^^^^^^^^^^^^
135+
|
136+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
137+
help: add parentheses around the `-` and the literal to call the method on a negative literal
138+
|
139+
LL | let _ = (-1.0_f64).sin().cos();
140+
| + +
141+
help: add parentheses around the literal and the method call to keep the current behavior
142+
|
143+
LL | let _ = -(1.0_f64.sin().cos());
144+
| + +
145+
146+
error: `-` has lower precedence than method calls, which might be unexpected
147+
--> $DIR/negative_literals.rs:22:13
148+
|
149+
LL | let _ = -1f64.sin().sin();
150+
| ^^^^^^^^^^^^^^^^^
151+
|
152+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
153+
help: add parentheses around the `-` and the literal to call the method on a negative literal
154+
|
155+
LL | let _ = (-1f64).sin().sin();
156+
| + +
157+
help: add parentheses around the literal and the method call to keep the current behavior
158+
|
159+
LL | let _ = -(1f64.sin().sin());
160+
| + +
161+
162+
error: `-` has lower precedence than method calls, which might be unexpected
163+
--> $DIR/negative_literals.rs:25:11
164+
|
165+
LL | dbg!( -1.0_f32.cos() );
166+
| ^^^^^^^^^^^^^^
167+
|
168+
= note: e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
169+
help: add parentheses around the `-` and the literal to call the method on a negative literal
170+
|
171+
LL | dbg!( (-1.0_f32).cos() );
172+
| + +
173+
help: add parentheses around the literal and the method call to keep the current behavior
174+
|
175+
LL | dbg!( -(1.0_f32.cos()) );
176+
| + +
177+
178+
error: aborting due to 11 previous errors
179+

0 commit comments

Comments
 (0)