Skip to content

Commit d3cbf72

Browse files
committed
implement lint double_negations
1 parent 6698ec9 commit d3cbf72

File tree

8 files changed

+150
-6
lines changed

8 files changed

+150
-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

+55-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::errors::BuiltinEllipsisInclusiveRangePatterns;
5050
use crate::lints::{
5151
BuiltinAnonymousParams, BuiltinConstNoMangle, BuiltinDeprecatedAttrLink,
5252
BuiltinDeprecatedAttrLinkSuggestion, BuiltinDeprecatedAttrUsed, BuiltinDerefNullptr,
53+
BuiltinDoubleNegations, BuiltinDoubleNegationsAddParens,
5354
BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives,
5455
BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures,
5556
BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents,
@@ -1574,6 +1575,58 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
15741575
}
15751576
}
15761577

1578+
declare_lint! {
1579+
/// The `double_negations` lint detects expressions of the form `--x`.
1580+
///
1581+
/// ### Example
1582+
///
1583+
/// ```rust
1584+
/// fn main() {
1585+
/// let x = 1;
1586+
/// let _b = --x;
1587+
/// }
1588+
/// ```
1589+
///
1590+
/// {{produces}}
1591+
///
1592+
/// ### Explanation
1593+
///
1594+
/// Negating something twice is usually the same as not negating it at all.
1595+
/// However, a double negation in Rust can easily be confused with the
1596+
/// prefix decrement operator that exists in many languages derived from C.
1597+
/// Use `-(-x)` if you really wanted to negate the value twice.
1598+
///
1599+
/// To decrement a value, use `x -= 1` instead.
1600+
pub DOUBLE_NEGATIONS,
1601+
Warn,
1602+
"detects expressions of the form `--x`"
1603+
}
1604+
1605+
declare_lint_pass!(
1606+
/// Lint for expressions of the form `--x` that can be confused with C's
1607+
/// prefix decrement operator.
1608+
DoubleNegations => [DOUBLE_NEGATIONS]
1609+
);
1610+
1611+
impl EarlyLintPass for DoubleNegations {
1612+
#[inline]
1613+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
1614+
// only lint on the innermost `--` in a chain of `-` operators,
1615+
// even if there are 3 or more negations
1616+
if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind
1617+
&& let ExprKind::Unary(UnOp::Neg, ref inner2) = inner.kind
1618+
&& !matches!(inner2.kind, ExprKind::Unary(UnOp::Neg, _))
1619+
{
1620+
cx.emit_span_lint(DOUBLE_NEGATIONS, expr.span, BuiltinDoubleNegations {
1621+
add_parens: BuiltinDoubleNegationsAddParens {
1622+
start_span: inner.span.shrink_to_lo(),
1623+
end_span: inner.span.shrink_to_hi(),
1624+
},
1625+
});
1626+
}
1627+
}
1628+
}
1629+
15771630
declare_lint_pass!(
15781631
/// Does nothing as a lint pass, but registers some `Lint`s
15791632
/// which are used by other parts of the compiler.
@@ -1592,7 +1645,8 @@ declare_lint_pass!(
15921645
UNSTABLE_FEATURES,
15931646
UNREACHABLE_PUB,
15941647
TYPE_ALIAS_BOUNDS,
1595-
TRIVIAL_BOUNDS
1648+
TRIVIAL_BOUNDS,
1649+
DOUBLE_NEGATIONS
15961650
]
15971651
);
15981652

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)