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 https://github.com/rust-lang/rust/pull/117161, without the binary op precedence. Fixes https://github.com/rust-lang/rust/issues/117155 `@rustbot` labels +I-lang-nominated cc `@scottmcm` r? compiler
This commit is contained in:
commit
ae71900ef6
12 changed files with 325 additions and 156 deletions
|
@ -5,6 +5,11 @@ lint_ambiguous_glob_reexport = ambiguous glob re-exports
|
|||
.label_first_reexport = the name `{$name}` in the {$namespace} namespace is first re-exported here
|
||||
.label_duplicate_reexport = but the name `{$name}` in the {$namespace} namespace is also re-exported here
|
||||
|
||||
lint_ambiguous_negative_literals = `-` has lower precedence than method calls, which might be unexpected
|
||||
.example = e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
|
||||
.negative_literal = add parentheses around the `-` and the literal to call the method on a negative literal
|
||||
.current_behavior = add parentheses around the literal and the method call to keep the current behavior
|
||||
|
||||
lint_ambiguous_wide_pointer_comparisons = ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
|
||||
.addr_metadata_suggestion = use explicit `std::ptr::eq` method to compare metadata and addresses
|
||||
.addr_suggestion = use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
|
||||
|
|
|
@ -73,6 +73,7 @@ mod noop_method_call;
|
|||
mod opaque_hidden_inferred_bound;
|
||||
mod pass_by_value;
|
||||
mod passes;
|
||||
mod precedence;
|
||||
mod ptr_nulls;
|
||||
mod redundant_semicolon;
|
||||
mod reference_casting;
|
||||
|
@ -111,6 +112,7 @@ use nonstandard_style::*;
|
|||
use noop_method_call::*;
|
||||
use opaque_hidden_inferred_bound::*;
|
||||
use pass_by_value::*;
|
||||
use precedence::*;
|
||||
use ptr_nulls::*;
|
||||
use redundant_semicolon::*;
|
||||
use reference_casting::*;
|
||||
|
@ -174,6 +176,7 @@ early_lint_methods!(
|
|||
RedundantSemicolons: RedundantSemicolons,
|
||||
UnusedDocComment: UnusedDocComment,
|
||||
Expr2024: Expr2024,
|
||||
Precedence: Precedence,
|
||||
]
|
||||
]
|
||||
);
|
||||
|
|
|
@ -1499,6 +1499,35 @@ pub struct NonLocalDefinitionsCargoUpdateNote {
|
|||
pub crate_name: Symbol,
|
||||
}
|
||||
|
||||
// precedence.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_ambiguous_negative_literals)]
|
||||
#[note(lint_example)]
|
||||
pub struct AmbiguousNegativeLiteralsDiag {
|
||||
#[subdiagnostic]
|
||||
pub negative_literal: AmbiguousNegativeLiteralsNegativeLiteralSuggestion,
|
||||
#[subdiagnostic]
|
||||
pub current_behavior: AmbiguousNegativeLiteralsCurrentBehaviorSuggestion,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
#[multipart_suggestion(lint_negative_literal, applicability = "maybe-incorrect")]
|
||||
pub struct AmbiguousNegativeLiteralsNegativeLiteralSuggestion {
|
||||
#[suggestion_part(code = "(")]
|
||||
pub start_span: Span,
|
||||
#[suggestion_part(code = ")")]
|
||||
pub end_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
#[multipart_suggestion(lint_current_behavior, applicability = "maybe-incorrect")]
|
||||
pub struct AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
|
||||
#[suggestion_part(code = "(")]
|
||||
pub start_span: Span,
|
||||
#[suggestion_part(code = ")")]
|
||||
pub end_span: Span,
|
||||
}
|
||||
|
||||
// pass_by_value.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_pass_by_value)]
|
||||
|
|
70
compiler/rustc_lint/src/precedence.rs
Normal file
70
compiler/rustc_lint/src/precedence.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use rustc_ast::token::LitKind;
|
||||
use rustc_ast::{Expr, ExprKind, MethodCall, UnOp};
|
||||
use rustc_session::{declare_lint, declare_lint_pass};
|
||||
|
||||
use crate::lints::{
|
||||
AmbiguousNegativeLiteralsCurrentBehaviorSuggestion, AmbiguousNegativeLiteralsDiag,
|
||||
AmbiguousNegativeLiteralsNegativeLiteralSuggestion,
|
||||
};
|
||||
use crate::{EarlyContext, EarlyLintPass, LintContext};
|
||||
|
||||
declare_lint! {
|
||||
/// 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
|
||||
/// # #![allow(unused)]
|
||||
/// -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1
|
||||
/// ```
|
||||
///
|
||||
/// {{produces}}
|
||||
///
|
||||
/// ### Explanation
|
||||
///
|
||||
/// Method calls take precedence over unary precedence. Setting the
|
||||
/// precedence explicitly makes the code clearer and avoid potential bugs.
|
||||
pub AMBIGUOUS_NEGATIVE_LITERALS,
|
||||
Deny,
|
||||
"ambiguous negative literals operations",
|
||||
report_in_external_macro
|
||||
}
|
||||
|
||||
declare_lint_pass!(Precedence => [AMBIGUOUS_NEGATIVE_LITERALS]);
|
||||
|
||||
impl EarlyLintPass for Precedence {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut arg = operand;
|
||||
let mut at_least_one = false;
|
||||
while let ExprKind::MethodCall(box MethodCall { receiver, .. }) = &arg.kind {
|
||||
at_least_one = true;
|
||||
arg = receiver;
|
||||
}
|
||||
|
||||
if at_least_one
|
||||
&& let ExprKind::Lit(lit) = &arg.kind
|
||||
&& let LitKind::Integer | LitKind::Float = &lit.kind
|
||||
{
|
||||
cx.emit_span_lint(
|
||||
AMBIGUOUS_NEGATIVE_LITERALS,
|
||||
expr.span,
|
||||
AmbiguousNegativeLiteralsDiag {
|
||||
negative_literal: AmbiguousNegativeLiteralsNegativeLiteralSuggestion {
|
||||
start_span: expr.span.shrink_to_lo(),
|
||||
end_span: arg.span.shrink_to_hi(),
|
||||
},
|
||||
current_behavior: AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
|
||||
start_span: operand.span.shrink_to_lo(),
|
||||
end_span: operand.span.shrink_to_hi(),
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue