Skip to content

Commit ae71900

Browse files
authored
Rollup merge of #121364 - Urgau:unary_precedence, r=compiler-errors
Implement lint against ambiguous negative literals This PR implements a lint against ambiguous negative literals with a literal and method calls right after it. ## `ambiguous_negative_literals` (deny-by-default) The `ambiguous_negative_literals` lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal. ### Example ```rust,compile_fail -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. <details> <summary>Old proposed lint</summary> ## `ambiguous_unary_precedence` (deny-by-default) The `ambiguous_unary_precedence` lint checks for use the negative unary operator with a literal and method calls. ### Example ```rust -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. </details> ----- Note: This is a strip down version of #117161, without the binary op precedence. Fixes #117155 `@rustbot` labels +I-lang-nominated cc `@scottmcm` r? compiler
2 parents 54be9ad + c5e1a12 commit ae71900

File tree

12 files changed

+325
-156
lines changed

12 files changed

+325
-156
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
@@ -73,6 +73,7 @@ mod noop_method_call;
7373
mod opaque_hidden_inferred_bound;
7474
mod pass_by_value;
7575
mod passes;
76+
mod precedence;
7677
mod ptr_nulls;
7778
mod redundant_semicolon;
7879
mod reference_casting;
@@ -111,6 +112,7 @@ use nonstandard_style::*;
111112
use noop_method_call::*;
112113
use opaque_hidden_inferred_bound::*;
113114
use pass_by_value::*;
115+
use precedence::*;
114116
use ptr_nulls::*;
115117
use redundant_semicolon::*;
116118
use reference_casting::*;
@@ -174,6 +176,7 @@ early_lint_methods!(
174176
RedundantSemicolons: RedundantSemicolons,
175177
UnusedDocComment: UnusedDocComment,
176178
Expr2024: Expr2024,
179+
Precedence: Precedence,
177180
]
178181
]
179182
);

compiler/rustc_lint/src/lints.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,35 @@ pub struct NonLocalDefinitionsCargoUpdateNote {
14991499
pub crate_name: Symbol,
15001500
}
15011501

1502+
// precedence.rs
1503+
#[derive(LintDiagnostic)]
1504+
#[diag(lint_ambiguous_negative_literals)]
1505+
#[note(lint_example)]
1506+
pub struct AmbiguousNegativeLiteralsDiag {
1507+
#[subdiagnostic]
1508+
pub negative_literal: AmbiguousNegativeLiteralsNegativeLiteralSuggestion,
1509+
#[subdiagnostic]
1510+
pub current_behavior: AmbiguousNegativeLiteralsCurrentBehaviorSuggestion,
1511+
}
1512+
1513+
#[derive(Subdiagnostic)]
1514+
#[multipart_suggestion(lint_negative_literal, applicability = "maybe-incorrect")]
1515+
pub struct AmbiguousNegativeLiteralsNegativeLiteralSuggestion {
1516+
#[suggestion_part(code = "(")]
1517+
pub start_span: Span,
1518+
#[suggestion_part(code = ")")]
1519+
pub end_span: Span,
1520+
}
1521+
1522+
#[derive(Subdiagnostic)]
1523+
#[multipart_suggestion(lint_current_behavior, applicability = "maybe-incorrect")]
1524+
pub struct AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
1525+
#[suggestion_part(code = "(")]
1526+
pub start_span: Span,
1527+
#[suggestion_part(code = ")")]
1528+
pub end_span: Span,
1529+
}
1530+
15021531
// pass_by_value.rs
15031532
#[derive(LintDiagnostic)]
15041533
#[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+
}

src/tools/clippy/clippy_lints/src/precedence.rs

+1-55
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp};
4-
use rustc_ast::token;
3+
use rustc_ast::ast::{BinOpKind, Expr, ExprKind};
54
use rustc_errors::Applicability;
65
use rustc_lint::{EarlyContext, EarlyLintPass};
76
use rustc_session::declare_lint_pass;
87
use rustc_span::source_map::Spanned;
98

10-
const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [
11-
"asin",
12-
"asinh",
13-
"atan",
14-
"atanh",
15-
"cbrt",
16-
"fract",
17-
"round",
18-
"signum",
19-
"sin",
20-
"sinh",
21-
"tan",
22-
"tanh",
23-
"to_degrees",
24-
"to_radians",
25-
];
26-
279
declare_clippy_lint! {
2810
/// ### What it does
2911
/// Checks for operations where precedence may be unclear
3012
/// and suggests to add parentheses. Currently it catches the following:
3113
/// * mixed usage of arithmetic and bit shifting/combining operators without
3214
/// parentheses
33-
/// * a "negative" numeric literal (which is really a unary `-` followed by a
34-
/// numeric literal)
35-
/// followed by a method call
3615
///
3716
/// ### Why is this bad?
3817
/// Not everyone knows the precedence of those operators by
@@ -41,7 +20,6 @@ declare_clippy_lint! {
4120
///
4221
/// ### Example
4322
/// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
44-
/// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
4523
#[clippy::version = "pre 1.29.0"]
4624
pub PRECEDENCE,
4725
complexity,
@@ -104,38 +82,6 @@ impl EarlyLintPass for Precedence {
10482
(false, false) => (),
10583
}
10684
}
107-
108-
if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
109-
let mut arg = operand;
110-
111-
let mut all_odd = true;
112-
while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind {
113-
let seg_str = seg.ident.name.as_str();
114-
all_odd &= ALLOWED_ODD_FUNCTIONS
115-
.iter()
116-
.any(|odd_function| **odd_function == *seg_str);
117-
arg = receiver;
118-
}
119-
120-
if !all_odd
121-
&& let ExprKind::Lit(lit) = &arg.kind
122-
&& let token::LitKind::Integer | token::LitKind::Float = &lit.kind
123-
{
124-
let mut applicability = Applicability::MachineApplicable;
125-
span_lint_and_sugg(
126-
cx,
127-
PRECEDENCE,
128-
expr.span,
129-
"unary minus has lower precedence than method call",
130-
"consider adding parentheses to clarify your intent",
131-
format!(
132-
"-({})",
133-
snippet_with_applicability(cx, operand.span, "..", &mut applicability)
134-
),
135-
applicability,
136-
);
137-
}
138-
}
13985
}
14086
}
14187

src/tools/clippy/tests/ui/precedence.fixed

-34
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,6 @@ fn main() {
2020
1 ^ (1 - 1);
2121
3 | (2 - 1);
2222
3 & (5 - 2);
23-
-(1i32.abs());
24-
-(1f32.abs());
25-
26-
// These should not trigger an error
27-
let _ = (-1i32).abs();
28-
let _ = (-1f32).abs();
29-
let _ = -(1i32).abs();
30-
let _ = -(1f32).abs();
31-
let _ = -(1i32.abs());
32-
let _ = -(1f32.abs());
33-
34-
// Odd functions should not trigger an error
35-
let _ = -1f64.asin();
36-
let _ = -1f64.asinh();
37-
let _ = -1f64.atan();
38-
let _ = -1f64.atanh();
39-
let _ = -1f64.cbrt();
40-
let _ = -1f64.fract();
41-
let _ = -1f64.round();
42-
let _ = -1f64.signum();
43-
let _ = -1f64.sin();
44-
let _ = -1f64.sinh();
45-
let _ = -1f64.tan();
46-
let _ = -1f64.tanh();
47-
let _ = -1f64.to_degrees();
48-
let _ = -1f64.to_radians();
49-
50-
// Chains containing any non-odd function should trigger (issue #5924)
51-
let _ = -(1.0_f64.cos().cos());
52-
let _ = -(1.0_f64.cos().sin());
53-
let _ = -(1.0_f64.sin().cos());
54-
55-
// Chains of odd functions shouldn't trigger
56-
let _ = -1f64.sin().sin();
5723

5824
let b = 3;
5925
trip!(b * 8);

src/tools/clippy/tests/ui/precedence.rs

-34
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,6 @@ fn main() {
2020
1 ^ 1 - 1;
2121
3 | 2 - 1;
2222
3 & 5 - 2;
23-
-1i32.abs();
24-
-1f32.abs();
25-
26-
// These should not trigger an error
27-
let _ = (-1i32).abs();
28-
let _ = (-1f32).abs();
29-
let _ = -(1i32).abs();
30-
let _ = -(1f32).abs();
31-
let _ = -(1i32.abs());
32-
let _ = -(1f32.abs());
33-
34-
// Odd functions should not trigger an error
35-
let _ = -1f64.asin();
36-
let _ = -1f64.asinh();
37-
let _ = -1f64.atan();
38-
let _ = -1f64.atanh();
39-
let _ = -1f64.cbrt();
40-
let _ = -1f64.fract();
41-
let _ = -1f64.round();
42-
let _ = -1f64.signum();
43-
let _ = -1f64.sin();
44-
let _ = -1f64.sinh();
45-
let _ = -1f64.tan();
46-
let _ = -1f64.tanh();
47-
let _ = -1f64.to_degrees();
48-
let _ = -1f64.to_radians();
49-
50-
// Chains containing any non-odd function should trigger (issue #5924)
51-
let _ = -1.0_f64.cos().cos();
52-
let _ = -1.0_f64.cos().sin();
53-
let _ = -1.0_f64.sin().cos();
54-
55-
// Chains of odd functions shouldn't trigger
56-
let _ = -1f64.sin().sin();
5723

5824
let b = 3;
5925
trip!(b * 8);

src/tools/clippy/tests/ui/precedence.stderr

+1-31
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,5 @@ error: operator precedence can trip the unwary
4343
LL | 3 & 5 - 2;
4444
| ^^^^^^^^^ help: consider parenthesizing your expression: `3 & (5 - 2)`
4545

46-
error: unary minus has lower precedence than method call
47-
--> tests/ui/precedence.rs:23:5
48-
|
49-
LL | -1i32.abs();
50-
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1i32.abs())`
51-
52-
error: unary minus has lower precedence than method call
53-
--> tests/ui/precedence.rs:24:5
54-
|
55-
LL | -1f32.abs();
56-
| ^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1f32.abs())`
57-
58-
error: unary minus has lower precedence than method call
59-
--> tests/ui/precedence.rs:51:13
60-
|
61-
LL | let _ = -1.0_f64.cos().cos();
62-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().cos())`
63-
64-
error: unary minus has lower precedence than method call
65-
--> tests/ui/precedence.rs:52:13
66-
|
67-
LL | let _ = -1.0_f64.cos().sin();
68-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.cos().sin())`
69-
70-
error: unary minus has lower precedence than method call
71-
--> tests/ui/precedence.rs:53:13
72-
|
73-
LL | let _ = -1.0_f64.sin().cos();
74-
| ^^^^^^^^^^^^^^^^^^^^ help: consider adding parentheses to clarify your intent: `-(1.0_f64.sin().cos())`
75-
76-
error: aborting due to 12 previous errors
46+
error: aborting due to 7 previous errors
7747

src/tools/clippy/tests/ui/unnecessary_cast.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ mod fixable {
206206

207207
fn issue_9563() {
208208
let _: f64 = (-8.0_f64).exp();
209-
#[allow(clippy::precedence)]
209+
#[allow(ambiguous_negative_literals)]
210210
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
211211
}
212212

src/tools/clippy/tests/ui/unnecessary_cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ mod fixable {
206206

207207
fn issue_9563() {
208208
let _: f64 = (-8.0 as f64).exp();
209-
#[allow(clippy::precedence)]
209+
#[allow(ambiguous_negative_literals)]
210210
let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
211211
}
212212

0 commit comments

Comments
 (0)