Skip to content

Commit

Permalink
Prefer language operator instead of the English terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Nov 17, 2023
1 parent a98d077 commit 0fd29ec
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ lint_path_statement_drop = path statement drops value
lint_path_statement_no_effect = path statement with no effect
lint_precedence_unary = unary minus has lower precedence than method call
lint_precedence_unary = unary operator `{$op}` has lower precedence than method call
.suggestion = consider adding parentheses to clarify your intent
lint_precedence_unwary = operator precedence can trip the unwary
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ pub struct ExpectationNote {
pub enum PrecedenceDiag {
#[diag(lint_precedence_unary)]
Unary {
op: &'static str,
#[subdiagnostic]
suggestion: PrecedenceUnarySuggestion,
},
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_lint/src/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl EarlyLintPass for Precedence {
);
}

if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind
if let ExprKind::Unary(unop, operand) = &expr.kind
&& matches!(unop, UnOp::Neg)
&& let ExprKind::MethodCall(..) = operand.kind
{
let mut arg = operand;
Expand All @@ -80,6 +81,7 @@ impl EarlyLintPass for Precedence {
&& !arg.span.from_expansion()
{
cx.emit_spanned_lint(AMBIGUOUS_PRECEDENCE, expr.span, PrecedenceDiag::Unary {
op: UnOp::to_string(*unop),
suggestion: PrecedenceUnarySuggestion {
start_span: operand.span.shrink_to_lo(),
end_span: operand.span.shrink_to_hi(),
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/lint/precedence.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ fn main() {
let _ = (1 + 2) << (3 + 1);
//~^ WARN operator precedence can trip the unwary
let _ = -(1i32.abs());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f32.abs());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f64.asin());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f64.asinh());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f64.tan());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f64.tanh());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1.0_f64.cos().cos());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1.0_f64.cos().sin());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1.0_f64.sin().cos());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f64.sin().sin());
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call

// These should not trigger an error
let _ = (-1i32).abs();
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/lint/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ fn main() {
let _ = 1 + 2 << 3 + 1;
//~^ WARN operator precedence can trip the unwary
let _ = -1i32.abs();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f32.abs();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f64.asin();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f64.asinh();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f64.tan();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f64.tanh();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1.0_f64.cos().cos();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1.0_f64.cos().sin();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1.0_f64.sin().cos();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f64.sin().sin();
//~^ WARN unary minus has lower precedence than method call
//~^ WARN unary operator `-` has lower precedence than method call

// These should not trigger an error
let _ = (-1i32).abs();
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/lint/precedence.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = (1 + 2) << (3 + 1);
| + + + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:21:13
|
LL | let _ = -1i32.abs();
Expand All @@ -98,7 +98,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1i32.abs());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:23:13
|
LL | let _ = -1f32.abs();
Expand All @@ -109,7 +109,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f32.abs());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:25:13
|
LL | let _ = -1f64.asin();
Expand All @@ -120,7 +120,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f64.asin());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:27:13
|
LL | let _ = -1f64.asinh();
Expand All @@ -131,7 +131,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f64.asinh());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:29:13
|
LL | let _ = -1f64.tan();
Expand All @@ -142,7 +142,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f64.tan());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:31:13
|
LL | let _ = -1f64.tanh();
Expand All @@ -153,7 +153,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f64.tanh());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:33:13
|
LL | let _ = -1.0_f64.cos().cos();
Expand All @@ -164,7 +164,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1.0_f64.cos().cos());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:35:13
|
LL | let _ = -1.0_f64.cos().sin();
Expand All @@ -175,7 +175,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1.0_f64.cos().sin());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:37:13
|
LL | let _ = -1.0_f64.sin().cos();
Expand All @@ -186,7 +186,7 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1.0_f64.sin().cos());
| + +

warning: unary minus has lower precedence than method call
warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:39:13
|
LL | let _ = -1f64.sin().sin();
Expand Down

0 comments on commit 0fd29ec

Please sign in to comment.