2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::ast_utils::{eq_id, is_useless_with_eq_exprs, IdentIter};
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2020-12-06 08:01:03 -06:00
|
|
|
use core::ops::{Add, AddAssign};
|
|
|
|
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, StmtKind};
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2020-12-06 08:01:03 -06:00
|
|
|
use rustc_span::source_map::Spanned;
|
|
|
|
use rustc_span::symbol::Ident;
|
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
2020-12-06 08:01:03 -06:00
|
|
|
/// Checks for unlikely usages of binary operators that are almost
|
|
|
|
/// certainly typos and/or copy/paste errors, given the other usages
|
|
|
|
/// of binary operators nearby.
|
2021-07-29 05:16:06 -05:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
2020-12-06 08:01:03 -06:00
|
|
|
/// They are probably bugs and if they aren't then they look like bugs
|
|
|
|
/// and you should add a comment explaining why you are doing such an
|
|
|
|
/// odd set of operations.
|
2021-07-29 05:16:06 -05:00
|
|
|
///
|
|
|
|
/// ### Known problems
|
2020-12-06 08:01:03 -06:00
|
|
|
/// There may be some false positives if you are trying to do something
|
|
|
|
/// unusual that happens to look like a typo.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2020-12-06 08:01:03 -06:00
|
|
|
/// struct Vec3 {
|
|
|
|
/// x: f64,
|
|
|
|
/// y: f64,
|
|
|
|
/// z: f64,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Eq for Vec3 {}
|
|
|
|
///
|
|
|
|
/// impl PartialEq for Vec3 {
|
|
|
|
/// fn eq(&self, other: &Self) -> bool {
|
|
|
|
/// // This should trigger the lint because `self.x` is compared to `other.y`
|
|
|
|
/// self.x == other.y && self.y == other.y && self.z == other.z
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2020-12-06 08:01:03 -06:00
|
|
|
/// # struct Vec3 {
|
|
|
|
/// # x: f64,
|
|
|
|
/// # y: f64,
|
|
|
|
/// # z: f64,
|
|
|
|
/// # }
|
|
|
|
/// // same as above except:
|
|
|
|
/// impl PartialEq for Vec3 {
|
|
|
|
/// fn eq(&self, other: &Self) -> bool {
|
|
|
|
/// // Note we now compare other.x to self.x
|
|
|
|
/// self.x == other.x && self.y == other.y && self.z == other.z
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.50.0"]
|
2020-12-06 08:01:03 -06:00
|
|
|
pub SUSPICIOUS_OPERATION_GROUPINGS,
|
2021-06-03 01:41:37 -05:00
|
|
|
nursery,
|
2020-12-06 08:01:03 -06:00
|
|
|
"groupings of binary operations that look suspiciously like typos"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(SuspiciousOperationGroupings => [SUSPICIOUS_OPERATION_GROUPINGS]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for SuspiciousOperationGroupings {
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
|
|
|
if expr.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(binops) = extract_related_binops(&expr.kind) {
|
|
|
|
check_binops(cx, &binops.iter().collect::<Vec<_>>());
|
|
|
|
|
|
|
|
let mut op_types = Vec::with_capacity(binops.len());
|
|
|
|
// We could use a hashmap, etc. to avoid being O(n*m) here, but
|
|
|
|
// we want the lints to be emitted in a consistent order. Besides,
|
|
|
|
// m, (the number of distinct `BinOpKind`s in `binops`)
|
|
|
|
// will often be small, and does have an upper limit.
|
|
|
|
binops.iter().map(|b| b.op).for_each(|op| {
|
|
|
|
if !op_types.contains(&op) {
|
|
|
|
op_types.push(op);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for op_type in op_types {
|
|
|
|
let ops: Vec<_> = binops.iter().filter(|b| b.op == op_type).collect();
|
|
|
|
|
|
|
|
check_binops(cx, &ops);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_binops(cx: &EarlyContext<'_>, binops: &[&BinaryOp<'_>]) {
|
|
|
|
let binop_count = binops.len();
|
|
|
|
if binop_count < 2 {
|
|
|
|
// Single binary operation expressions would likely be false
|
|
|
|
// positives.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut one_ident_difference_count = 0;
|
|
|
|
let mut no_difference_info = None;
|
|
|
|
let mut double_difference_info = None;
|
|
|
|
let mut expected_ident_loc = None;
|
|
|
|
|
|
|
|
let mut paired_identifiers = FxHashSet::default();
|
|
|
|
|
|
|
|
for (i, BinaryOp { left, right, op, .. }) in binops.iter().enumerate() {
|
|
|
|
match ident_difference_expr(left, right) {
|
|
|
|
IdentDifference::NoDifference => {
|
|
|
|
if is_useless_with_eq_exprs(*op) {
|
|
|
|
// The `eq_op` lint should catch this in this case.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
no_difference_info = Some(i);
|
|
|
|
},
|
|
|
|
IdentDifference::Single(ident_loc) => {
|
|
|
|
one_ident_difference_count += 1;
|
|
|
|
if let Some(previous_expected) = expected_ident_loc {
|
|
|
|
if previous_expected != ident_loc {
|
|
|
|
// This expression doesn't match the form we're
|
|
|
|
// looking for.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expected_ident_loc = Some(ident_loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was only a single difference, all other idents
|
|
|
|
// must have been the same, and thus were paired.
|
|
|
|
for id in skip_index(IdentIter::from(*left), ident_loc.index) {
|
|
|
|
paired_identifiers.insert(id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
IdentDifference::Double(ident_loc1, ident_loc2) => {
|
|
|
|
double_difference_info = Some((i, ident_loc1, ident_loc2));
|
|
|
|
},
|
|
|
|
IdentDifference::Multiple | IdentDifference::NonIdent => {
|
|
|
|
// It's too hard to know whether this is a bug or not.
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
|
|
|
|
if let Some(expected_loc) = expected_ident_loc {
|
|
|
|
match (no_difference_info, double_difference_info) {
|
|
|
|
(Some(i), None) => attempt_to_emit_no_difference_lint(cx, binops, i, expected_loc),
|
|
|
|
(None, Some((double_difference_index, ident_loc1, ident_loc2))) => {
|
2023-11-16 12:13:24 -06:00
|
|
|
if one_ident_difference_count == binop_count - 1
|
|
|
|
&& let Some(binop) = binops.get(double_difference_index)
|
|
|
|
{
|
|
|
|
let changed_loc = if ident_loc1 == expected_loc {
|
|
|
|
ident_loc2
|
|
|
|
} else if ident_loc2 == expected_loc {
|
|
|
|
ident_loc1
|
|
|
|
} else {
|
|
|
|
// This expression doesn't match the form we're
|
|
|
|
// looking for.
|
|
|
|
return;
|
|
|
|
};
|
2020-12-06 08:01:03 -06:00
|
|
|
|
2023-11-16 12:13:24 -06:00
|
|
|
if let Some(sugg) = ident_swap_sugg(cx, &paired_identifiers, binop, changed_loc, &mut applicability)
|
|
|
|
{
|
|
|
|
emit_suggestion(cx, binop.span, sugg, applicability);
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn attempt_to_emit_no_difference_lint(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
binops: &[&BinaryOp<'_>],
|
|
|
|
i: usize,
|
|
|
|
expected_loc: IdentLocation,
|
|
|
|
) {
|
2021-04-22 04:31:13 -05:00
|
|
|
if let Some(binop) = binops.get(i).copied() {
|
2020-12-06 08:01:03 -06:00
|
|
|
// We need to try and figure out which identifier we should
|
|
|
|
// suggest using instead. Since there could be multiple
|
|
|
|
// replacement candidates in a given expression, and we're
|
|
|
|
// just taking the first one, we may get some bad lint
|
|
|
|
// messages.
|
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
|
|
|
|
|
|
|
// We assume that the correct ident is one used elsewhere in
|
|
|
|
// the other binops, in a place that there was a single
|
|
|
|
// difference between idents before.
|
|
|
|
let old_left_ident = get_ident(binop.left, expected_loc);
|
|
|
|
let old_right_ident = get_ident(binop.right, expected_loc);
|
|
|
|
|
|
|
|
for b in skip_index(binops.iter(), i) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let (Some(old_ident), Some(new_ident)) = (old_left_ident, get_ident(b.left, expected_loc))
|
|
|
|
&& old_ident != new_ident
|
|
|
|
&& let Some(sugg) =
|
|
|
|
suggestion_with_swapped_ident(cx, binop.left, expected_loc, new_ident, &mut applicability)
|
|
|
|
{
|
|
|
|
emit_suggestion(
|
2020-12-06 08:01:03 -06:00
|
|
|
cx,
|
2023-11-16 12:13:24 -06:00
|
|
|
binop.span,
|
|
|
|
replace_left_sugg(cx, binop, &sugg, &mut applicability),
|
|
|
|
applicability,
|
2020-12-06 08:01:03 -06:00
|
|
|
);
|
2023-11-16 12:13:24 -06:00
|
|
|
return;
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
|
2023-11-16 12:13:24 -06:00
|
|
|
if let (Some(old_ident), Some(new_ident)) = (old_right_ident, get_ident(b.right, expected_loc))
|
|
|
|
&& old_ident != new_ident
|
|
|
|
&& let Some(sugg) =
|
|
|
|
suggestion_with_swapped_ident(cx, binop.right, expected_loc, new_ident, &mut applicability)
|
|
|
|
{
|
|
|
|
emit_suggestion(
|
2020-12-06 08:01:03 -06:00
|
|
|
cx,
|
2023-11-16 12:13:24 -06:00
|
|
|
binop.span,
|
|
|
|
replace_right_sugg(cx, binop, &sugg, &mut applicability),
|
|
|
|
applicability,
|
2020-12-06 08:01:03 -06:00
|
|
|
);
|
2023-11-16 12:13:24 -06:00
|
|
|
return;
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_suggestion(cx: &EarlyContext<'_>, span: Span, sugg: String, applicability: Applicability) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
SUSPICIOUS_OPERATION_GROUPINGS,
|
|
|
|
span,
|
2021-03-12 08:30:50 -06:00
|
|
|
"this sequence of operators looks suspiciously like a bug",
|
|
|
|
"did you mean",
|
2020-12-06 08:01:03 -06:00
|
|
|
sugg,
|
|
|
|
applicability,
|
2021-06-03 01:41:37 -05:00
|
|
|
);
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ident_swap_sugg(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
paired_identifiers: &FxHashSet<Ident>,
|
|
|
|
binop: &BinaryOp<'_>,
|
|
|
|
location: IdentLocation,
|
|
|
|
applicability: &mut Applicability,
|
|
|
|
) -> Option<String> {
|
2021-04-08 10:50:13 -05:00
|
|
|
let left_ident = get_ident(binop.left, location)?;
|
|
|
|
let right_ident = get_ident(binop.right, location)?;
|
2020-12-06 08:01:03 -06:00
|
|
|
|
|
|
|
let sugg = match (
|
|
|
|
paired_identifiers.contains(&left_ident),
|
|
|
|
paired_identifiers.contains(&right_ident),
|
|
|
|
) {
|
|
|
|
(true, true) | (false, false) => {
|
|
|
|
// We don't have a good guess of what ident should be
|
|
|
|
// used instead, in these cases.
|
|
|
|
*applicability = Applicability::MaybeIncorrect;
|
|
|
|
|
2022-03-16 07:12:30 -05:00
|
|
|
// We arbitrarily choose one side to suggest changing,
|
2020-12-06 08:01:03 -06:00
|
|
|
// since we don't have a better guess. If the user
|
|
|
|
// ends up duplicating a clause, the `logic_bug` lint
|
|
|
|
// should catch it.
|
|
|
|
|
2021-04-08 10:50:13 -05:00
|
|
|
let right_suggestion = suggestion_with_swapped_ident(cx, binop.right, location, left_ident, applicability)?;
|
2020-12-06 08:01:03 -06:00
|
|
|
|
|
|
|
replace_right_sugg(cx, binop, &right_suggestion, applicability)
|
|
|
|
},
|
|
|
|
(false, true) => {
|
|
|
|
// We haven't seen a pair involving the left one, so
|
|
|
|
// it's probably what is wanted.
|
|
|
|
|
2021-04-08 10:50:13 -05:00
|
|
|
let right_suggestion = suggestion_with_swapped_ident(cx, binop.right, location, left_ident, applicability)?;
|
2020-12-06 08:01:03 -06:00
|
|
|
|
|
|
|
replace_right_sugg(cx, binop, &right_suggestion, applicability)
|
|
|
|
},
|
|
|
|
(true, false) => {
|
|
|
|
// We haven't seen a pair involving the right one, so
|
|
|
|
// it's probably what is wanted.
|
2021-04-08 10:50:13 -05:00
|
|
|
let left_suggestion = suggestion_with_swapped_ident(cx, binop.left, location, right_ident, applicability)?;
|
2020-12-06 08:01:03 -06:00
|
|
|
|
|
|
|
replace_left_sugg(cx, binop, &left_suggestion, applicability)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(sugg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_left_sugg(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
binop: &BinaryOp<'_>,
|
|
|
|
left_suggestion: &str,
|
|
|
|
applicability: &mut Applicability,
|
|
|
|
) -> String {
|
|
|
|
format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"{left_suggestion} {} {}",
|
2023-11-27 16:11:03 -06:00
|
|
|
binop.op.as_str(),
|
2020-12-06 08:01:03 -06:00
|
|
|
snippet_with_applicability(cx, binop.right.span, "..", applicability),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_right_sugg(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
binop: &BinaryOp<'_>,
|
|
|
|
right_suggestion: &str,
|
|
|
|
applicability: &mut Applicability,
|
|
|
|
) -> String {
|
|
|
|
format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"{} {} {right_suggestion}",
|
2020-12-06 08:01:03 -06:00
|
|
|
snippet_with_applicability(cx, binop.left.span, "..", applicability),
|
2023-11-27 16:11:03 -06:00
|
|
|
binop.op.as_str(),
|
2020-12-06 08:01:03 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct BinaryOp<'exprs> {
|
|
|
|
op: BinOpKind,
|
|
|
|
span: Span,
|
|
|
|
left: &'exprs Expr,
|
|
|
|
right: &'exprs Expr,
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
impl<'exprs> BinaryOp<'exprs> {
|
2020-12-06 08:01:03 -06:00
|
|
|
fn new(op: BinOpKind, span: Span, (left, right): (&'exprs Expr, &'exprs Expr)) -> Self {
|
|
|
|
Self { op, span, left, right }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strip_non_ident_wrappers(expr: &Expr) -> &Expr {
|
|
|
|
let mut output = expr;
|
|
|
|
loop {
|
|
|
|
output = match &output.kind {
|
|
|
|
ExprKind::Paren(ref inner) | ExprKind::Unary(_, ref inner) => inner,
|
|
|
|
_ => {
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_related_binops(kind: &ExprKind) -> Option<Vec<BinaryOp<'_>>> {
|
2022-03-16 07:12:30 -05:00
|
|
|
append_opt_vecs(chained_binops(kind), if_statement_binops(kind))
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
|
2022-03-16 07:12:30 -05:00
|
|
|
fn if_statement_binops(kind: &ExprKind) -> Option<Vec<BinaryOp<'_>>> {
|
2020-12-06 08:01:03 -06:00
|
|
|
match kind {
|
|
|
|
ExprKind::If(ref condition, _, _) => chained_binops(&condition.kind),
|
2022-03-16 07:12:30 -05:00
|
|
|
ExprKind::Paren(ref e) => if_statement_binops(&e.kind),
|
2020-12-06 08:01:03 -06:00
|
|
|
ExprKind::Block(ref block, _) => {
|
|
|
|
let mut output = None;
|
|
|
|
for stmt in &block.stmts {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Expr(ref e) | StmtKind::Semi(ref e) => {
|
2022-03-16 07:12:30 -05:00
|
|
|
output = append_opt_vecs(output, if_statement_binops(&e.kind));
|
2020-12-06 08:01:03 -06:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_opt_vecs<A>(target_opt: Option<Vec<A>>, source_opt: Option<Vec<A>>) -> Option<Vec<A>> {
|
|
|
|
match (target_opt, source_opt) {
|
2022-03-14 06:02:53 -05:00
|
|
|
(Some(mut target), Some(source)) => {
|
2020-12-06 08:01:03 -06:00
|
|
|
target.reserve(source.len());
|
2022-03-14 06:02:53 -05:00
|
|
|
for op in source {
|
2020-12-06 08:01:03 -06:00
|
|
|
target.push(op);
|
|
|
|
}
|
|
|
|
Some(target)
|
|
|
|
},
|
|
|
|
(Some(v), None) | (None, Some(v)) => Some(v),
|
|
|
|
(None, None) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chained_binops(kind: &ExprKind) -> Option<Vec<BinaryOp<'_>>> {
|
|
|
|
match kind {
|
|
|
|
ExprKind::Binary(_, left_outer, right_outer) => chained_binops_helper(left_outer, right_outer),
|
|
|
|
ExprKind::Paren(ref e) | ExprKind::Unary(_, ref e) => chained_binops(&e.kind),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
fn chained_binops_helper<'expr>(left_outer: &'expr Expr, right_outer: &'expr Expr) -> Option<Vec<BinaryOp<'expr>>> {
|
2020-12-06 08:01:03 -06:00
|
|
|
match (&left_outer.kind, &right_outer.kind) {
|
|
|
|
(
|
|
|
|
ExprKind::Paren(ref left_e) | ExprKind::Unary(_, ref left_e),
|
|
|
|
ExprKind::Paren(ref right_e) | ExprKind::Unary(_, ref right_e),
|
|
|
|
) => chained_binops_helper(left_e, right_e),
|
|
|
|
(ExprKind::Paren(ref left_e) | ExprKind::Unary(_, ref left_e), _) => chained_binops_helper(left_e, right_outer),
|
|
|
|
(_, ExprKind::Paren(ref right_e) | ExprKind::Unary(_, ref right_e)) => {
|
|
|
|
chained_binops_helper(left_outer, right_e)
|
|
|
|
},
|
|
|
|
(
|
|
|
|
ExprKind::Binary(Spanned { node: left_op, .. }, ref left_left, ref left_right),
|
|
|
|
ExprKind::Binary(Spanned { node: right_op, .. }, ref right_left, ref right_right),
|
|
|
|
) => match (
|
|
|
|
chained_binops_helper(left_left, left_right),
|
|
|
|
chained_binops_helper(right_left, right_right),
|
|
|
|
) {
|
2022-03-14 06:02:53 -05:00
|
|
|
(Some(mut left_ops), Some(right_ops)) => {
|
2020-12-06 08:01:03 -06:00
|
|
|
left_ops.reserve(right_ops.len());
|
2022-03-14 06:02:53 -05:00
|
|
|
for op in right_ops {
|
2020-12-06 08:01:03 -06:00
|
|
|
left_ops.push(op);
|
|
|
|
}
|
|
|
|
Some(left_ops)
|
|
|
|
},
|
|
|
|
(Some(mut left_ops), _) => {
|
|
|
|
left_ops.push(BinaryOp::new(*right_op, right_outer.span, (right_left, right_right)));
|
|
|
|
Some(left_ops)
|
|
|
|
},
|
|
|
|
(_, Some(mut right_ops)) => {
|
|
|
|
right_ops.insert(0, BinaryOp::new(*left_op, left_outer.span, (left_left, left_right)));
|
|
|
|
Some(right_ops)
|
|
|
|
},
|
|
|
|
(None, None) => Some(vec![
|
|
|
|
BinaryOp::new(*left_op, left_outer.span, (left_left, left_right)),
|
|
|
|
BinaryOp::new(*right_op, right_outer.span, (right_left, right_right)),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
|
|
|
|
struct IdentLocation {
|
|
|
|
index: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for IdentLocation {
|
|
|
|
type Output = IdentLocation;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self::Output {
|
|
|
|
Self {
|
|
|
|
index: self.index + other.index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign for IdentLocation {
|
|
|
|
fn add_assign(&mut self, other: Self) {
|
2021-06-03 01:41:37 -05:00
|
|
|
*self = *self + other;
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
enum IdentDifference {
|
|
|
|
NoDifference,
|
|
|
|
Single(IdentLocation),
|
|
|
|
Double(IdentLocation, IdentLocation),
|
|
|
|
Multiple,
|
|
|
|
NonIdent,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for IdentDifference {
|
|
|
|
type Output = IdentDifference;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self::Output {
|
|
|
|
match (self, other) {
|
|
|
|
(Self::NoDifference, output) | (output, Self::NoDifference) => output,
|
|
|
|
(Self::Multiple, _)
|
|
|
|
| (_, Self::Multiple)
|
|
|
|
| (Self::Double(_, _), Self::Single(_))
|
|
|
|
| (Self::Single(_) | Self::Double(_, _), Self::Double(_, _)) => Self::Multiple,
|
|
|
|
(Self::NonIdent, _) | (_, Self::NonIdent) => Self::NonIdent,
|
|
|
|
(Self::Single(il1), Self::Single(il2)) => Self::Double(il1, il2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign for IdentDifference {
|
|
|
|
fn add_assign(&mut self, other: Self) {
|
2021-06-03 01:41:37 -05:00
|
|
|
*self = *self + other;
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentDifference {
|
|
|
|
/// Returns true if learning about more differences will not change the value
|
|
|
|
/// of this `IdentDifference`, and false otherwise.
|
|
|
|
fn is_complete(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::NoDifference | Self::Single(_) | Self::Double(_, _) => false,
|
|
|
|
Self::Multiple | Self::NonIdent => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident_difference_expr(left: &Expr, right: &Expr) -> IdentDifference {
|
|
|
|
ident_difference_expr_with_base_location(left, right, IdentLocation::default()).0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident_difference_expr_with_base_location(
|
|
|
|
left: &Expr,
|
|
|
|
right: &Expr,
|
|
|
|
mut base: IdentLocation,
|
|
|
|
) -> (IdentDifference, IdentLocation) {
|
|
|
|
// Ideally, this function should not use IdentIter because it should return
|
|
|
|
// early if the expressions have any non-ident differences. We want that early
|
|
|
|
// return because if without that restriction the lint would lead to false
|
|
|
|
// positives.
|
|
|
|
//
|
|
|
|
// But, we cannot (easily?) use a `rustc_ast::visit::Visitor`, since we need
|
|
|
|
// the two expressions to be walked in lockstep. And without a `Visitor`, we'd
|
|
|
|
// have to do all the AST traversal ourselves, which is a lot of work, since to
|
|
|
|
// do it properly we'd need to be able to handle more or less every possible
|
|
|
|
// AST node since `Item`s can be written inside `Expr`s.
|
|
|
|
//
|
|
|
|
// In practice, it seems likely that expressions, above a certain size, that
|
|
|
|
// happen to use the exact same idents in the exact same order, and which are
|
|
|
|
// not structured the same, would be rare. Therefore it seems likely that if
|
|
|
|
// we do only the first layer of matching ourselves and eventually fallback on
|
|
|
|
// IdentIter, then the output of this function will be almost always be correct
|
|
|
|
// in practice.
|
|
|
|
//
|
2022-05-05 09:12:52 -05:00
|
|
|
// If it turns out that problematic cases are more prevalent than we assume,
|
2020-12-06 08:01:03 -06:00
|
|
|
// then we should be able to change this function to do the correct traversal,
|
|
|
|
// without needing to change the rest of the code.
|
|
|
|
|
|
|
|
#![allow(clippy::enum_glob_use)]
|
|
|
|
use ExprKind::*;
|
|
|
|
|
|
|
|
match (
|
|
|
|
&strip_non_ident_wrappers(left).kind,
|
|
|
|
&strip_non_ident_wrappers(right).kind,
|
|
|
|
) {
|
|
|
|
(Yield(_), Yield(_))
|
|
|
|
| (Try(_), Try(_))
|
|
|
|
| (Paren(_), Paren(_))
|
|
|
|
| (Repeat(_, _), Repeat(_, _))
|
2021-03-15 19:15:53 -05:00
|
|
|
| (Struct(_), Struct(_))
|
2020-12-06 08:01:03 -06:00
|
|
|
| (MacCall(_), MacCall(_))
|
|
|
|
| (InlineAsm(_), InlineAsm(_))
|
|
|
|
| (Ret(_), Ret(_))
|
|
|
|
| (Continue(_), Continue(_))
|
|
|
|
| (Break(_, _), Break(_, _))
|
|
|
|
| (AddrOf(_, _, _), AddrOf(_, _, _))
|
|
|
|
| (Path(_, _), Path(_, _))
|
|
|
|
| (Range(_, _, _), Range(_, _, _))
|
2023-08-03 14:43:17 -05:00
|
|
|
| (Index(_, _, _), Index(_, _, _))
|
2020-12-06 08:01:03 -06:00
|
|
|
| (Field(_, _), Field(_, _))
|
|
|
|
| (AssignOp(_, _, _), AssignOp(_, _, _))
|
|
|
|
| (Assign(_, _, _), Assign(_, _, _))
|
|
|
|
| (TryBlock(_), TryBlock(_))
|
2023-04-25 14:52:17 -05:00
|
|
|
| (Await(_, _), Await(_, _))
|
2023-10-20 16:26:57 -05:00
|
|
|
| (Gen(_, _, _), Gen(_, _, _))
|
2020-12-06 08:01:03 -06:00
|
|
|
| (Block(_, _), Block(_, _))
|
2022-09-07 19:52:51 -05:00
|
|
|
| (Closure(_), Closure(_))
|
2024-02-17 11:43:54 -06:00
|
|
|
| (Match(_, _, _), Match(_, _, _))
|
2022-11-03 11:19:23 -05:00
|
|
|
| (Loop(_, _, _), Loop(_, _, _))
|
2023-12-08 16:51:50 -06:00
|
|
|
| (ForLoop { .. }, ForLoop { .. })
|
2020-12-06 08:01:03 -06:00
|
|
|
| (While(_, _, _), While(_, _, _))
|
|
|
|
| (If(_, _, _), If(_, _, _))
|
2023-09-08 11:14:47 -05:00
|
|
|
| (Let(_, _, _, _), Let(_, _, _, _))
|
2020-12-06 08:01:03 -06:00
|
|
|
| (Type(_, _), Type(_, _))
|
|
|
|
| (Cast(_, _), Cast(_, _))
|
|
|
|
| (Lit(_), Lit(_))
|
|
|
|
| (Unary(_, _), Unary(_, _))
|
|
|
|
| (Binary(_, _, _), Binary(_, _, _))
|
|
|
|
| (Tup(_), Tup(_))
|
2022-09-07 19:52:51 -05:00
|
|
|
| (MethodCall(_), MethodCall(_))
|
2020-12-06 08:01:03 -06:00
|
|
|
| (Call(_, _), Call(_, _))
|
|
|
|
| (ConstBlock(_), ConstBlock(_))
|
2023-02-27 07:17:29 -06:00
|
|
|
| (Array(_), Array(_)) => {
|
2020-12-06 08:01:03 -06:00
|
|
|
// keep going
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return (IdentDifference::NonIdent, base);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut difference = IdentDifference::NoDifference;
|
|
|
|
|
|
|
|
for (left_attr, right_attr) in left.attrs.iter().zip(right.attrs.iter()) {
|
|
|
|
let (new_difference, new_base) =
|
|
|
|
ident_difference_via_ident_iter_with_base_location(left_attr, right_attr, base);
|
|
|
|
base = new_base;
|
|
|
|
difference += new_difference;
|
|
|
|
if difference.is_complete() {
|
|
|
|
return (difference, base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (new_difference, new_base) = ident_difference_via_ident_iter_with_base_location(left, right, base);
|
|
|
|
base = new_base;
|
|
|
|
difference += new_difference;
|
|
|
|
|
|
|
|
(difference, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident_difference_via_ident_iter_with_base_location<Iterable: Into<IdentIter>>(
|
|
|
|
left: Iterable,
|
|
|
|
right: Iterable,
|
|
|
|
mut base: IdentLocation,
|
|
|
|
) -> (IdentDifference, IdentLocation) {
|
|
|
|
// See the note in `ident_difference_expr_with_base_location` about `IdentIter`
|
|
|
|
let mut difference = IdentDifference::NoDifference;
|
|
|
|
|
|
|
|
let mut left_iterator = left.into();
|
|
|
|
let mut right_iterator = right.into();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match (left_iterator.next(), right_iterator.next()) {
|
|
|
|
(Some(left_ident), Some(right_ident)) => {
|
|
|
|
if !eq_id(left_ident, right_ident) {
|
|
|
|
difference += IdentDifference::Single(base);
|
|
|
|
if difference.is_complete() {
|
|
|
|
return (difference, base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(Some(_), None) | (None, Some(_)) => {
|
|
|
|
return (IdentDifference::NonIdent, base);
|
|
|
|
},
|
|
|
|
(None, None) => {
|
|
|
|
return (difference, base);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
base += IdentLocation { index: 1 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_ident(expr: &Expr, location: IdentLocation) -> Option<Ident> {
|
|
|
|
IdentIter::from(expr).nth(location.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn suggestion_with_swapped_ident(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
expr: &Expr,
|
|
|
|
location: IdentLocation,
|
|
|
|
new_ident: Ident,
|
|
|
|
applicability: &mut Applicability,
|
|
|
|
) -> Option<String> {
|
|
|
|
get_ident(expr, location).and_then(|current_ident| {
|
|
|
|
if eq_id(current_ident, new_ident) {
|
|
|
|
// We never want to suggest a non-change
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"{}{new_ident}{}",
|
2020-12-06 08:01:03 -06:00
|
|
|
snippet_with_applicability(cx, expr.span.with_hi(current_ident.span.lo()), "..", applicability),
|
|
|
|
snippet_with_applicability(cx, expr.span.with_lo(current_ident.span.hi()), "..", applicability),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn skip_index<A, Iter>(iter: Iter, index: usize) -> impl Iterator<Item = A>
|
|
|
|
where
|
|
|
|
Iter: Iterator<Item = A>,
|
|
|
|
{
|
|
|
|
iter.enumerate()
|
|
|
|
.filter_map(move |(i, a)| if i == index { None } else { Some(a) })
|
|
|
|
}
|