Skip to content

Commit b5b6945

Browse files
authored
Merge pull request #1061 from Manishearth/double_neg
new lint: double_neg
2 parents d6e3fa8 + b731802 commit b5b6945

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
All notable changes to this project will be documented in this file.
33

44
## 0.0.78 - TBA
5-
* New lints: [`wrong_transmute`]
5+
* New lints: [`wrong_transmute`, `double_neg`]
66
* For compatibility, `cargo clippy` does not defines the `clippy` feature
77
introduced in 0.0.76 anymore
88
* [`collapsible_if`] now considers `if let`
@@ -153,6 +153,7 @@ All notable changes to this project will be documented in this file.
153153
[`deprecated_semver`]: https://github.com/Manishearth/rust-clippy/wiki#deprecated_semver
154154
[`derive_hash_xor_eq`]: https://github.com/Manishearth/rust-clippy/wiki#derive_hash_xor_eq
155155
[`doc_markdown`]: https://github.com/Manishearth/rust-clippy/wiki#doc_markdown
156+
[`double_neg`]: https://github.com/Manishearth/rust-clippy/wiki#double_neg
156157
[`drop_ref`]: https://github.com/Manishearth/rust-clippy/wiki#drop_ref
157158
[`duplicate_underscore_argument`]: https://github.com/Manishearth/rust-clippy/wiki#duplicate_underscore_argument
158159
[`empty_loop`]: https://github.com/Manishearth/rust-clippy/wiki#empty_loop

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Table of contents:
1717

1818
## Lints
1919

20-
There are 156 lints included in this crate:
20+
There are 157 lints included in this crate:
2121

2222
name | default | meaning
2323
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -49,6 +49,7 @@ name
4949
[deprecated_semver](https://github.com/Manishearth/rust-clippy/wiki#deprecated_semver) | warn | `Warn` on `#[deprecated(since = "x")]` where x is not semver
5050
[derive_hash_xor_eq](https://github.com/Manishearth/rust-clippy/wiki#derive_hash_xor_eq) | warn | deriving `Hash` but implementing `PartialEq` explicitly
5151
[doc_markdown](https://github.com/Manishearth/rust-clippy/wiki#doc_markdown) | warn | checks for the presence of `_`, `::` or camel-case outside ticks in documentation
52+
[double_neg](https://github.com/Manishearth/rust-clippy/wiki#double_neg) | warn | `--x` is a double negation of `x` and not a pre-decrement as in C or C++
5253
[drop_ref](https://github.com/Manishearth/rust-clippy/wiki#drop_ref) | warn | call to `std::mem::drop` with a reference instead of an owned value, which will not call the `Drop::drop` method on the underlying value
5354
[duplicate_underscore_argument](https://github.com/Manishearth/rust-clippy/wiki#duplicate_underscore_argument) | warn | Function arguments having names which only differ by an underscore
5455
[empty_loop](https://github.com/Manishearth/rust-clippy/wiki#empty_loop) | warn | empty `loop {}` detected

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
369369
misc::MODULO_ONE,
370370
misc::REDUNDANT_PATTERN,
371371
misc::TOPLEVEL_REF_ARG,
372+
misc_early::DOUBLE_NEG,
372373
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
373374
misc_early::REDUNDANT_CLOSURE_CALL,
374375
misc_early::UNNEEDED_FIELD_PATTERN,

clippy_lints/src/misc_early.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ declare_lint! {
4040
"Closures should not be called in the expression they are defined"
4141
}
4242

43+
/// **What it does:** This lint detects expressions of the form `--x`
44+
///
45+
/// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
46+
///
47+
/// **Known problems:** None.
48+
///
49+
/// **Example:** `--x;`
50+
declare_lint! {
51+
pub DOUBLE_NEG, Warn,
52+
"`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
53+
}
54+
55+
4356
#[derive(Copy, Clone)]
4457
pub struct MiscEarly;
4558

4659
impl LintPass for MiscEarly {
4760
fn get_lints(&self) -> LintArray {
48-
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL)
61+
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG)
4962
}
5063
}
5164

@@ -126,21 +139,32 @@ impl EarlyLintPass for MiscEarly {
126139
}
127140

128141
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
129-
if let ExprKind::Call(ref paren, _) = expr.node {
130-
if let ExprKind::Paren(ref closure) = paren.node {
131-
if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
132-
span_lint_and_then(cx,
133-
REDUNDANT_CLOSURE_CALL,
134-
expr.span,
135-
"Try not to call a closure in the expression where it is declared.",
136-
|db| {
137-
if decl.inputs.is_empty() {
138-
let hint = format!("{}", snippet(cx, block.span, ".."));
139-
db.span_suggestion(expr.span, "Try doing something like: ", hint);
140-
}
141-
});
142+
match expr.node {
143+
ExprKind::Call(ref paren, _) => {
144+
if let ExprKind::Paren(ref closure) = paren.node {
145+
if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
146+
span_lint_and_then(cx,
147+
REDUNDANT_CLOSURE_CALL,
148+
expr.span,
149+
"Try not to call a closure in the expression where it is declared.",
150+
|db| {
151+
if decl.inputs.is_empty() {
152+
let hint = format!("{}", snippet(cx, block.span, ".."));
153+
db.span_suggestion(expr.span, "Try doing something like: ", hint);
154+
}
155+
});
156+
}
142157
}
143158
}
159+
ExprKind::Unary(UnOp::Neg, ref inner) => {
160+
if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
161+
span_lint(cx,
162+
DOUBLE_NEG,
163+
expr.span,
164+
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
165+
}
166+
}
167+
_ => ()
144168
}
145169
}
146170

tests/compile-fail/double_neg.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#[deny(double_neg)]
5+
fn main() {
6+
let x = 1;
7+
-x;
8+
-(-x);
9+
--x; //~ERROR: `--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op
10+
}

0 commit comments

Comments
 (0)