2022-06-04 19:44:37 +00:00
|
|
|
use clippy_utils::{
|
|
|
|
diagnostics::span_lint_and_then,
|
|
|
|
higher,
|
2022-06-04 20:08:33 +00:00
|
|
|
source::{snippet, snippet_with_applicability},
|
2022-06-04 19:44:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use rustc_ast::ast;
|
2022-06-03 14:12:26 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-06-04 19:44:37 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
|
|
|
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-06-03 14:12:26 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2022-06-04 17:37:28 +00:00
|
|
|
/// The lint checks for parenthesis on literals in range statements that are
|
2022-06-05 19:41:00 +00:00
|
|
|
/// superfluous.
|
2022-06-03 14:12:26 +00:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
2022-06-05 19:41:00 +00:00
|
|
|
/// Having superfluous parenthesis makes the code less readable
|
2022-06-03 14:12:26 +00:00
|
|
|
/// overhead when reading.
|
2022-06-04 19:48:44 +00:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// for i in (0)..10 {
|
|
|
|
/// println!("{i}");
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// for i in 0..10 {
|
|
|
|
/// println!("{i}");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-06-03 14:12:26 +00:00
|
|
|
#[clippy::version = "1.63.0"]
|
2022-06-05 20:12:30 +00:00
|
|
|
pub NEEDLESS_PARENS_ON_RANGE_LITERALS,
|
2022-06-03 14:12:26 +00:00
|
|
|
style,
|
2022-06-05 20:12:30 +00:00
|
|
|
"needless parenthesis on range literals can be removed"
|
2022-06-03 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:12:30 +00:00
|
|
|
declare_lint_pass!(NeedlessParensOnRangeLiterals => [NEEDLESS_PARENS_ON_RANGE_LITERALS]);
|
2022-06-03 14:12:26 +00:00
|
|
|
|
2022-06-04 19:44:37 +00:00
|
|
|
fn snippet_enclosed_in_parenthesis(snippet: &str) -> bool {
|
|
|
|
snippet.starts_with('(') && snippet.ends_with(')')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
|
|
|
|
if is_start &&
|
|
|
|
let ExprKind::Lit(ref literal) = e.kind &&
|
|
|
|
let ast::LitKind::Float(_sym, ast::LitFloatType::Unsuffixed) = literal.node
|
|
|
|
{
|
|
|
|
// don't check floating point literals on the start expression of a range
|
|
|
|
return;
|
2022-06-03 14:12:26 +00:00
|
|
|
}
|
2022-06-04 19:44:37 +00:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Lit(ref literal) = e.kind;
|
2022-06-05 19:41:00 +00:00
|
|
|
// the indicator that parenthesis surround the literal is that the span of the expression and the literal differ
|
2022-06-04 19:44:37 +00:00
|
|
|
if (literal.span.data().hi - literal.span.data().lo) != (e.span.data().hi - e.span.data().lo);
|
|
|
|
// inspect the source code of the expression for parenthesis
|
|
|
|
if snippet_enclosed_in_parenthesis(&snippet(cx, e.span, ""));
|
|
|
|
then {
|
2022-06-04 20:08:33 +00:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2022-06-05 20:12:30 +00:00
|
|
|
span_lint_and_then(cx, NEEDLESS_PARENS_ON_RANGE_LITERALS, e.span,
|
|
|
|
"needless parenthesis on range literals can be removed",
|
2022-06-04 19:44:37 +00:00
|
|
|
|diag| {
|
2022-06-04 20:08:33 +00:00
|
|
|
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability);
|
|
|
|
diag.span_suggestion(e.span, "try", suggestion, applicability);
|
2022-06-04 19:44:37 +00:00
|
|
|
});
|
|
|
|
}
|
2022-06-03 14:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:12:30 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for NeedlessParensOnRangeLiterals {
|
2022-06-04 19:44:37 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-06-04 20:01:52 +00:00
|
|
|
if let Some(higher::Range { start, end, .. }) = higher::Range::hir(expr) {
|
|
|
|
if let Some(start) = start {
|
|
|
|
check_for_parens(cx, start, true);
|
|
|
|
}
|
|
|
|
if let Some(end) = end {
|
|
|
|
check_for_parens(cx, end, false);
|
|
|
|
}
|
2022-06-03 14:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|