rust/clippy_lints/src/double_parens.rs

75 lines
2.1 KiB
Rust
Raw Normal View History

use clippy_utils::diagnostics::span_lint;
2020-03-01 12:23:33 +09:00
use rustc_ast::ast::{Expr, ExprKind};
2020-01-12 15:08:41 +09:00
use rustc_lint::{EarlyContext, EarlyLintPass};
2020-01-11 20:37:08 +09:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
2018-03-28 15:24:26 +02:00
declare_clippy_lint! {
/// ### What it does
/// Checks for unnecessary double parentheses.
///
/// ### Why is this bad?
/// This makes code harder to read and might indicate a
/// mistake.
///
/// ### Example
/// ```rust
/// fn simple_double_parens() -> i32 {
/// ((0))
/// }
///
/// # fn foo(bar: usize) {}
/// foo((0));
/// ```
///
/// Use instead:
/// ```rust
/// fn simple_no_parens() -> i32 {
/// 0
/// }
///
/// # fn foo(bar: usize) {}
/// foo(0);
/// ```
#[clippy::version = "pre 1.29.0"]
2018-03-28 15:24:26 +02:00
pub DOUBLE_PARENS,
complexity,
2016-12-28 10:54:23 -08:00
"Warn on unnecessary double parentheses"
}
2019-04-08 13:43:55 -07:00
declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]);
2016-12-28 10:54:23 -08:00
impl EarlyLintPass for DoubleParens {
2018-07-23 13:01:12 +02:00
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
2019-08-19 09:30:32 -07:00
if expr.span.from_expansion() {
return;
}
let msg: &str = "consider removing unnecessary double parentheses";
2019-09-27 17:16:06 +02:00
match expr.kind {
ExprKind::Paren(ref in_paren) => match in_paren.kind {
2017-09-05 11:33:04 +02:00
ExprKind::Paren(_) | ExprKind::Tup(_) => {
span_lint(cx, DOUBLE_PARENS, expr.span, msg);
2017-09-05 11:33:04 +02:00
},
_ => {},
2016-12-28 12:03:49 -08:00
},
2018-11-27 21:14:15 +01:00
ExprKind::Call(_, ref params) => {
if params.len() == 1 {
let param = &params[0];
2019-09-27 17:16:06 +02:00
if let ExprKind::Paren(_) = param.kind {
span_lint(cx, DOUBLE_PARENS, param.span, msg);
2018-11-27 21:14:15 +01:00
}
2016-12-28 12:03:49 -08:00
}
},
ExprKind::MethodCall(ref call) => {
if let [ref arg] = call.args[..] {
if let ExprKind::Paren(_) = arg.kind {
span_lint(cx, DOUBLE_PARENS, arg.span, msg);
2018-11-27 21:14:15 +01:00
}
2016-12-28 12:03:49 -08:00
}
},
_ => {},
}
2016-12-28 10:54:23 -08:00
}
}