Merge pull request #1406 from theemathas/double-parens

Add double_parens lint
This commit is contained in:
Manish Goregaokar 2016-12-28 12:32:04 -08:00 committed by GitHub
commit 5d78485a81
8 changed files with 122 additions and 4 deletions

View File

@ -268,6 +268,7 @@ All notable changes to this project will be documented in this file.
[`diverging_sub_expression`]: https://github.com/Manishearth/rust-clippy/wiki#diverging_sub_expression
[`doc_markdown`]: https://github.com/Manishearth/rust-clippy/wiki#doc_markdown
[`double_neg`]: https://github.com/Manishearth/rust-clippy/wiki#double_neg
[`double_parens`]: https://github.com/Manishearth/rust-clippy/wiki#double_parens
[`drop_ref`]: https://github.com/Manishearth/rust-clippy/wiki#drop_ref
[`duplicate_underscore_argument`]: https://github.com/Manishearth/rust-clippy/wiki#duplicate_underscore_argument
[`empty_loop`]: https://github.com/Manishearth/rust-clippy/wiki#empty_loop

View File

@ -179,7 +179,7 @@ transparently:
## Lints
There are 180 lints included in this crate:
There are 181 lints included in this crate:
name | default | triggers on
-----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@ -216,6 +216,7 @@ name
[diverging_sub_expression](https://github.com/Manishearth/rust-clippy/wiki#diverging_sub_expression) | warn | whether an expression contains a diverging sub expression
[doc_markdown](https://github.com/Manishearth/rust-clippy/wiki#doc_markdown) | warn | presence of `_`, `::` or camel-case outside backticks in documentation
[double_neg](https://github.com/Manishearth/rust-clippy/wiki#double_neg) | warn | `--x`, which is a double negation of `x` and not a pre-decrement as in C/C++
[double_parens](https://github.com/Manishearth/rust-clippy/wiki#double_parens) | warn | Warn on unnecessary double parentheses
[drop_ref](https://github.com/Manishearth/rust-clippy/wiki#drop_ref) | warn | calls to `std::mem::drop` with a reference instead of an owned value
[duplicate_underscore_argument](https://github.com/Manishearth/rust-clippy/wiki#duplicate_underscore_argument) | warn | function arguments having names which only differ by an underscore
[empty_loop](https://github.com/Manishearth/rust-clippy/wiki#empty_loop) | warn | empty `loop {}`, which should block or sleep

View File

@ -0,0 +1,62 @@
use syntax::ast::*;
use rustc::lint::{EarlyContext, LintContext, LintArray, LintPass, EarlyLintPass};
/// **What it does:** Checks for unnecessary double parentheses.
///
/// **Why is this bad?** This makes code harder to read and might indicate a
/// mistake.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ((0))
/// foo((0))
/// ((1, 2))
/// ```
declare_lint! {
pub DOUBLE_PARENS, Warn,
"Warn on unnecessary double parentheses"
}
#[derive(Copy, Clone)]
pub struct DoubleParens;
impl LintPass for DoubleParens {
fn get_lints(&self) -> LintArray {
lint_array!(DOUBLE_PARENS)
}
}
impl EarlyLintPass for DoubleParens {
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
match expr.node {
ExprKind::Paren(ref in_paren) => {
match in_paren.node {
ExprKind::Paren(_) |
ExprKind::Tup(_) => {
cx.span_lint(DOUBLE_PARENS, expr.span, "Consider removing unnecessary double parentheses");
},
_ => {},
}
},
ExprKind::Call(_, ref params) => {
if params.len() == 1 {
let param = &params[0];
if let ExprKind::Paren(_) = param.node {
cx.span_lint(DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");
}
}
},
ExprKind::MethodCall(_, _, ref params) => {
if params.len() == 2 {
let param = &params[1];
if let ExprKind::Paren(_) = param.node {
cx.span_lint(DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");
}
}
},
_ => {},
}
}
}

View File

@ -69,6 +69,7 @@ pub mod copies;
pub mod cyclomatic_complexity;
pub mod derive;
pub mod doc;
pub mod double_parens;
pub mod drop_ref;
pub mod entry;
pub mod enum_clike;
@ -283,6 +284,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
reg.register_late_lint_pass(box if_let_redundant_pattern_matching::Pass);
reg.register_late_lint_pass(box partialeq_ne_impl::Pass);
reg.register_early_lint_pass(box reference::Pass);
reg.register_early_lint_pass(box double_parens::DoubleParens);
reg.register_lint_group("clippy_restrictions", vec![
arithmetic::FLOAT_ARITHMETIC,
@ -355,6 +357,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
derive::DERIVE_HASH_XOR_EQ,
derive::EXPL_IMPL_CLONE_ON_COPY,
doc::DOC_MARKDOWN,
double_parens::DOUBLE_PARENS,
drop_ref::DROP_REF,
entry::MAP_ENTRY,
enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT,

View File

@ -0,0 +1,51 @@
#![feature(plugin)]
#![plugin(clippy)]
#![deny(double_parens)]
#![allow(dead_code)]
fn dummy_fn<T>(_: T) {}
struct DummyStruct;
impl DummyStruct {
fn dummy_method<T>(self, _: T) {}
}
fn simple_double_parens() -> i32 {
((0)) //~ERROR Consider removing unnecessary double parentheses
}
fn fn_double_parens() {
dummy_fn((0)); //~ERROR Consider removing unnecessary double parentheses
}
fn method_double_parens(x: DummyStruct) {
x.dummy_method((0)); //~ERROR Consider removing unnecessary double parentheses
}
fn tuple_double_parens() -> (i32, i32) {
((1, 2)) //~ERROR Consider removing unnecessary double parentheses
}
fn unit_double_parens() {
(()) //~ERROR Consider removing unnecessary double parentheses
}
fn fn_tuple_ok() {
dummy_fn((1, 2));
}
fn method_tuple_ok(x: DummyStruct) {
x.dummy_method((1, 2));
}
fn fn_unit_ok() {
dummy_fn(());
}
fn method_unit_ok(x: DummyStruct) {
x.dummy_method(());
}
fn main() {}

View File

@ -2,7 +2,7 @@
#![plugin(clippy)]
#[deny(eq_op)]
#[allow(identity_op)]
#[allow(identity_op, double_parens)]
#[allow(no_effect, unused_variables, unnecessary_operation)]
#[deny(nonminimal_bool)]
fn main() {

View File

@ -5,7 +5,7 @@ const ONE : i64 = 1;
const NEG_ONE : i64 = -1;
const ZERO : i64 = 0;
#[allow(eq_op, no_effect, unnecessary_operation)]
#[allow(eq_op, no_effect, unnecessary_operation, double_parens)]
#[deny(identity_op)]
fn main() {
let x = 0;

View File

@ -9,7 +9,7 @@ fn get_reference(n : &usize) -> &usize {
n
}
#[allow(many_single_char_names)]
#[allow(many_single_char_names, double_parens)]
#[allow(unused_variables)]
#[deny(deref_addrof)]
fn main() {