Skip to content

Commit beb1dd2

Browse files
committed
implement lint double_negations
1 parent 0c33372 commit beb1dd2

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
@@ -51,6 +51,7 @@ use crate::errors::BuiltinEllipsisInclusiveRangePatterns;
5151
use crate::lints::{
5252
BuiltinAnonymousParams, BuiltinConstNoMangle, BuiltinDeprecatedAttrLink,
5353
BuiltinDeprecatedAttrLinkSuggestion, BuiltinDeprecatedAttrUsed, BuiltinDerefNullptr,
54+
BuiltinDoubleNegations, BuiltinDoubleNegationsAddParens,
5455
BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives,
5556
BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures,
5657
BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents,
@@ -1555,6 +1556,62 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
15551556
}
15561557
}
15571558

1559+
declare_lint! {
1560+
/// The `double_negations` lint detects expressions of the form `--x`.
1561+
///
1562+
/// ### Example
1563+
///
1564+
/// ```rust
1565+
/// fn main() {
1566+
/// let x = 1;
1567+
/// let _b = --x;
1568+
/// }
1569+
/// ```
1570+
///
1571+
/// {{produces}}
1572+
///
1573+
/// ### Explanation
1574+
///
1575+
/// Negating something twice is usually the same as not negating it at all.
1576+
/// However, a double negation in Rust can easily be confused with the
1577+
/// prefix decrement operator that exists in many languages derived from C.
1578+
/// Use `-(-x)` if you really wanted to negate the value twice.
1579+
///
1580+
/// To decrement a value, use `x -= 1` instead.
1581+
pub DOUBLE_NEGATIONS,
1582+
Warn,
1583+
"detects expressions of the form `--x`"
1584+
}
1585+
1586+
declare_lint_pass!(
1587+
/// Lint for expressions of the form `--x` that can be confused with C's
1588+
/// prefix decrement operator.
1589+
DoubleNegations => [DOUBLE_NEGATIONS]
1590+
);
1591+
1592+
impl EarlyLintPass for DoubleNegations {
1593+
#[inline]
1594+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
1595+
// only lint on the innermost `--` in a chain of `-` operators,
1596+
// even if there are 3 or more negations
1597+
if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind
1598+
&& let ExprKind::Unary(UnOp::Neg, ref inner2) = inner.kind
1599+
&& !matches!(inner2.kind, ExprKind::Unary(UnOp::Neg, _))
1600+
{
1601+
cx.emit_span_lint(
1602+
DOUBLE_NEGATIONS,
1603+
expr.span,
1604+
BuiltinDoubleNegations {
1605+
add_parens: BuiltinDoubleNegationsAddParens {
1606+
start_span: inner.span.shrink_to_lo(),
1607+
end_span: inner.span.shrink_to_hi(),
1608+
},
1609+
},
1610+
);
1611+
}
1612+
}
1613+
}
1614+
15581615
declare_lint_pass!(
15591616
/// Does nothing as a lint pass, but registers some `Lint`s
15601617
/// which are used by other parts of the compiler.
@@ -1573,7 +1630,8 @@ declare_lint_pass!(
15731630
UNSTABLE_FEATURES,
15741631
UNREACHABLE_PUB,
15751632
TYPE_ALIAS_BOUNDS,
1576-
TRIVIAL_BOUNDS
1633+
TRIVIAL_BOUNDS,
1634+
DOUBLE_NEGATIONS
15771635
]
15781636
);
15791637

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)