Skip to content

Commit 9862f05

Browse files
committed
implement lint double_negations
1 parent 326405f commit 9862f05

File tree

8 files changed

+154
-6
lines changed

8 files changed

+154
-6
lines changed

compiler/rustc_lint/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ lint_builtin_deprecated_attr_used = use of deprecated attribute `{$name}`: no lo
7878
lint_builtin_deref_nullptr = dereferencing a null pointer
7979
.label = this code causes undefined behavior when executed
8080
81+
lint_builtin_double_negations = use of a double negation
82+
.note = the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
83+
.note_decrement = use `-= 1` if you meant to decrement the value
84+
.add_parens_suggestion = add parentheses for clarity
85+
8186
lint_builtin_ellipsis_inclusive_range_patterns = `...` range patterns are deprecated
8287
.suggestion = use `..=` for an inclusive range
8388

compiler/rustc_lint/src/builtin.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use crate::errors::BuiltinEllipsisInclusiveRangePatterns;
5858
use crate::lints::{
5959
BuiltinAnonymousParams, BuiltinConstNoMangle, BuiltinDeprecatedAttrLink,
6060
BuiltinDeprecatedAttrLinkSuggestion, BuiltinDeprecatedAttrUsed, BuiltinDerefNullptr,
61+
BuiltinDoubleNegations, BuiltinDoubleNegationsAddParens,
6162
BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives,
6263
BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures,
6364
BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents,
@@ -1590,6 +1591,62 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
15901591
}
15911592
}
15921593

1594+
declare_lint! {
1595+
/// The `double_negations` lint detects expressions of the form `--x`.
1596+
///
1597+
/// ### Example
1598+
///
1599+
/// ```rust
1600+
/// fn main() {
1601+
/// let x = 1;
1602+
/// let _b = --x;
1603+
/// }
1604+
/// ```
1605+
///
1606+
/// {{produces}}
1607+
///
1608+
/// ### Explanation
1609+
///
1610+
/// Negating something twice is usually the same as not negating it at all.
1611+
/// However, a double negation in Rust can easily be confused with the
1612+
/// prefix decrement operator that exists in many languages derived from C.
1613+
/// Use `-(-x)` if you really wanted to negate the value twice.
1614+
///
1615+
/// To decrement a value, use `x -= 1` instead.
1616+
pub DOUBLE_NEGATIONS,
1617+
Warn,
1618+
"detects expressions of the form `--x`"
1619+
}
1620+
1621+
declare_lint_pass!(
1622+
/// Lint for expressions of the form `--x` that can be confused with C's
1623+
/// prefix decrement operator.
1624+
DoubleNegations => [DOUBLE_NEGATIONS]
1625+
);
1626+
1627+
impl EarlyLintPass for DoubleNegations {
1628+
#[inline]
1629+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
1630+
// only lint on the innermost `--` in a chain of `-` operators,
1631+
// even if there are 3 or more negations
1632+
if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind
1633+
&& let ExprKind::Unary(UnOp::Neg, ref inner2) = inner.kind
1634+
&& !matches!(inner2.kind, ExprKind::Unary(UnOp::Neg, _))
1635+
{
1636+
cx.emit_span_lint(
1637+
DOUBLE_NEGATIONS,
1638+
expr.span,
1639+
BuiltinDoubleNegations {
1640+
add_parens: BuiltinDoubleNegationsAddParens {
1641+
start_span: inner.span.shrink_to_lo(),
1642+
end_span: inner.span.shrink_to_hi(),
1643+
},
1644+
},
1645+
);
1646+
}
1647+
}
1648+
}
1649+
15931650
declare_lint_pass!(
15941651
/// Does nothing as a lint pass, but registers some `Lint`s
15951652
/// which are used by other parts of the compiler.
@@ -1608,7 +1665,8 @@ declare_lint_pass!(
16081665
UNSTABLE_FEATURES,
16091666
UNREACHABLE_PUB,
16101667
TYPE_ALIAS_BOUNDS,
1611-
TRIVIAL_BOUNDS
1668+
TRIVIAL_BOUNDS,
1669+
DOUBLE_NEGATIONS
16121670
]
16131671
);
16141672

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ early_lint_methods!(
181181
UnusedDocComment: UnusedDocComment,
182182
Expr2024: Expr2024,
183183
Precedence: Precedence,
184+
DoubleNegations: DoubleNegations,
184185
]
185186
]
186187
);

compiler/rustc_lint/src/lints.rs

+18
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,24 @@ pub(crate) struct BuiltinTrivialBounds<'a> {
343343
pub predicate: Clause<'a>,
344344
}
345345

346+
#[derive(LintDiagnostic)]
347+
#[diag(lint_builtin_double_negations)]
348+
#[note(lint_note)]
349+
#[note(lint_note_decrement)]
350+
pub(crate) struct BuiltinDoubleNegations {
351+
#[subdiagnostic]
352+
pub add_parens: BuiltinDoubleNegationsAddParens,
353+
}
354+
355+
#[derive(Subdiagnostic)]
356+
#[multipart_suggestion(lint_add_parens_suggestion, applicability = "maybe-incorrect")]
357+
pub(crate) struct BuiltinDoubleNegationsAddParens {
358+
#[suggestion_part(code = "(")]
359+
pub start_span: Span,
360+
#[suggestion_part(code = ")")]
361+
pub end_span: Span,
362+
}
363+
346364
#[derive(LintDiagnostic)]
347365
pub(crate) enum BuiltinEllipsisInclusiveRangePatternsLint {
348366
#[diag(lint_builtin_ellipsis_inclusive_range_patterns)]
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ check-pass
2+
fn main() {
3+
let x = 1;
4+
-x;
5+
-(-x);
6+
--x; //~ WARN use of a double negation
7+
---x; //~ WARN use of a double negation
8+
let _y = --(-x); //~ WARN use of a double negation
9+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
warning: use of a double negation
2+
--> $DIR/lint-double-negations.rs:6:5
3+
|
4+
LL | --x;
5+
| ^^^
6+
|
7+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
8+
= note: use `-= 1` if you meant to decrement the value
9+
= note: `#[warn(double_negations)]` on by default
10+
help: add parentheses for clarity
11+
|
12+
LL | -(-x);
13+
| + +
14+
15+
warning: use of a double negation
16+
--> $DIR/lint-double-negations.rs:7:6
17+
|
18+
LL | ---x;
19+
| ^^^
20+
|
21+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
22+
= note: use `-= 1` if you meant to decrement the value
23+
help: add parentheses for clarity
24+
|
25+
LL | --(-x);
26+
| + +
27+
28+
warning: use of a double negation
29+
--> $DIR/lint-double-negations.rs:8:14
30+
|
31+
LL | let _y = --(-x);
32+
| ^^^^^^
33+
|
34+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
35+
= note: use `-= 1` if you meant to decrement the value
36+
help: add parentheses for clarity
37+
|
38+
LL | let _y = -(-(-x));
39+
| + +
40+
41+
warning: 3 warnings emitted
42+

tests/ui/lint/lint-type-overflow2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
fn main() {
66
let x2: i8 = --128; //~ ERROR literal out of range for `i8`
7+
//~| WARN use of a double negation
78

89
let x = -3.40282357e+38_f32; //~ ERROR literal out of range for `f32`
910
let x = 3.40282357e+38_f32; //~ ERROR literal out of range for `f32`

tests/ui/lint/lint-type-overflow2.stderr

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
warning: use of a double negation
2+
--> $DIR/lint-type-overflow2.rs:6:18
3+
|
4+
LL | let x2: i8 = --128;
5+
| ^^^^^
6+
|
7+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
8+
= note: use `-= 1` if you meant to decrement the value
9+
= note: `#[warn(double_negations)]` on by default
10+
help: add parentheses for clarity
11+
|
12+
LL | let x2: i8 = -(-128);
13+
| + +
14+
115
error: literal out of range for `i8`
216
--> $DIR/lint-type-overflow2.rs:6:20
317
|
@@ -13,36 +27,36 @@ LL | #![deny(overflowing_literals)]
1327
| ^^^^^^^^^^^^^^^^^^^^
1428

1529
error: literal out of range for `f32`
16-
--> $DIR/lint-type-overflow2.rs:8:14
30+
--> $DIR/lint-type-overflow2.rs:9:14
1731
|
1832
LL | let x = -3.40282357e+38_f32;
1933
| ^^^^^^^^^^^^^^^^^^
2034
|
2135
= note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `f32::INFINITY`
2236

2337
error: literal out of range for `f32`
24-
--> $DIR/lint-type-overflow2.rs:9:14
38+
--> $DIR/lint-type-overflow2.rs:10:14
2539
|
2640
LL | let x = 3.40282357e+38_f32;
2741
| ^^^^^^^^^^^^^^^^^^
2842
|
2943
= note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `f32::INFINITY`
3044

3145
error: literal out of range for `f64`
32-
--> $DIR/lint-type-overflow2.rs:10:14
46+
--> $DIR/lint-type-overflow2.rs:11:14
3347
|
3448
LL | let x = -1.7976931348623159e+308_f64;
3549
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3650
|
3751
= note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `f64::INFINITY`
3852

3953
error: literal out of range for `f64`
40-
--> $DIR/lint-type-overflow2.rs:11:14
54+
--> $DIR/lint-type-overflow2.rs:12:14
4155
|
4256
LL | let x = 1.7976931348623159e+308_f64;
4357
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4458
|
4559
= note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `f64::INFINITY`
4660

47-
error: aborting due to 5 previous errors
61+
error: aborting due to 5 previous errors; 1 warning emitted
4862

0 commit comments

Comments
 (0)