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
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue