2021-03-25 19:29:11 +01:00
|
|
|
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-09-22 17:20:34 +02:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for unnecessary double parentheses.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This makes code harder to read and might indicate a
|
2019-03-05 11:50:33 -05:00
|
|
|
/// mistake.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```rust
|
2020-06-09 14:36:01 +00:00
|
|
|
/// fn simple_double_parens() -> i32 {
|
|
|
|
/// ((0))
|
|
|
|
/// }
|
|
|
|
///
|
2022-06-04 13:34:07 +02:00
|
|
|
/// # fn foo(bar: usize) {}
|
|
|
|
/// foo((0));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-06-09 14:36:01 +00:00
|
|
|
/// fn simple_no_parens() -> i32 {
|
|
|
|
/// 0
|
|
|
|
/// }
|
|
|
|
///
|
2019-08-02 08:13:54 +02:00
|
|
|
/// # fn foo(bar: usize) {}
|
2020-06-09 14:36:01 +00:00
|
|
|
/// foo(0);
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[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() {
|
2018-09-22 17:20:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:43:21 +02:00
|
|
|
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(_) => {
|
2021-04-08 17:50:13 +02:00
|
|
|
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 = ¶ms[0];
|
2019-09-27 17:16:06 +02:00
|
|
|
if let ExprKind::Paren(_) = param.kind {
|
2021-04-08 17:50:13 +02:00
|
|
|
span_lint(cx, DOUBLE_PARENS, param.span, msg);
|
2018-11-27 21:14:15 +01:00
|
|
|
}
|
2016-12-28 12:03:49 -08:00
|
|
|
}
|
|
|
|
},
|
2022-09-08 10:52:51 +10: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
|
|
|
}
|
|
|
|
}
|