Skip to content

new multi_assignment lint #9379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3897,6 +3897,7 @@ Released 2018-09-13
[`module_name_repetitions`]: https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions
[`modulo_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#modulo_arithmetic
[`modulo_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#modulo_one
[`multi_assignments`]: https://rust-lang.github.io/rust-clippy/master/index.html#multi_assignments
[`multiple_crate_versions`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions
[`multiple_inherent_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_inherent_impl
[`must_use_candidate`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
LintId::of(misc_early::UNNEEDED_WILDCARD_PATTERN),
LintId::of(misc_early::ZERO_PREFIXED_LITERAL),
LintId::of(mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION),
LintId::of(multi_assignments::MULTI_ASSIGNMENTS),
LintId::of(mut_key::MUTABLE_KEY_TYPE),
LintId::of(mut_reference::UNNECESSARY_MUT_PASSED),
LintId::of(needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ store.register_lints(&[
mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION,
module_style::MOD_MODULE_FILES,
module_style::SELF_NAMED_MODULE_FILES,
multi_assignments::MULTI_ASSIGNMENTS,
mut_key::MUTABLE_KEY_TYPE,
mut_mut::MUT_MUT,
mut_reference::UNNECESSARY_MUT_PASSED,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_suspicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
LintId::of(loops::MUT_RANGE_BOUND),
LintId::of(methods::NO_EFFECT_REPLACE),
LintId::of(methods::SUSPICIOUS_MAP),
LintId::of(multi_assignments::MULTI_ASSIGNMENTS),
LintId::of(mut_key::MUTABLE_KEY_TYPE),
LintId::of(octal_escapes::OCTAL_ESCAPES),
LintId::of(operators::FLOAT_EQUALITY_WITHOUT_ABS),
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ mod missing_enforced_import_rename;
mod missing_inline;
mod mixed_read_write_in_expression;
mod module_style;
mod multi_assignments;
mod mut_key;
mod mut_mut;
mod mut_reference;
Expand Down Expand Up @@ -896,6 +897,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(partialeq_to_none::PartialeqToNone));
store.register_late_pass(|| Box::new(manual_empty_string_creations::ManualEmptyStringCreations));
store.register_late_pass(|| Box::new(unused_peekable::UnusedPeekable));
store.register_early_pass(|| Box::new(multi_assignments::MultiAssignments));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
65 changes: 65 additions & 0 deletions clippy_lints/src/multi_assignments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use clippy_utils::diagnostics::span_lint;
use rustc_ast::ast::{Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// ### What it does
/// Checks for nested assignments.
///
/// ### Why is this bad?
/// While this is in most cases already a type mismatch,
/// the result of an assignment being `()` can throw off people coming from languages like python or C,
/// where such assignments return a copy of the assigned value.
///
/// ### Example
/// ```rust
///# let (a, b);
/// a = b = 42;
/// ```
/// Use instead:
/// ```rust
///# let (a, b);
/// b = 42;
/// a = b;
/// ```
#[clippy::version = "1.65.0"]
pub MULTI_ASSIGNMENTS,
suspicious,
"instead of using `a = b = c;` use `a = c; b = c;`"
}

declare_lint_pass!(MultiAssignments => [MULTI_ASSIGNMENTS]);

fn strip_paren_blocks(expr: &Expr) -> &Expr {
match &expr.kind {
ExprKind::Paren(e) => strip_paren_blocks(e),
ExprKind::Block(b, _) => {
if let [
Stmt {
kind: StmtKind::Expr(e),
..
},
] = &b.stmts[..]
{
strip_paren_blocks(e)
} else {
expr
}
},
_ => expr,
}
}

impl EarlyLintPass for MultiAssignments {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::Assign(target, source, _) = &expr.kind {
if let ExprKind::Assign(_target, _source, _) = &strip_paren_blocks(target).kind {
span_lint(cx, MULTI_ASSIGNMENTS, expr.span, "assignments don't nest intuitively");
};
if let ExprKind::Assign(_target, _source, _) = &strip_paren_blocks(source).kind {
span_lint(cx, MULTI_ASSIGNMENTS, expr.span, "assignments don't nest intuitively");
}
};
}
}
9 changes: 9 additions & 0 deletions tests/ui/multi_assignments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![warn(clippy::multi_assignments)]
fn main() {
let (mut a, mut b, mut c, mut d) = ((), (), (), ());
a = b = c;
a = b = c = d;
a = b = { c };
a = { b = c };
a = (b = c);
}
40 changes: 40 additions & 0 deletions tests/ui/multi_assignments.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:4:5
|
LL | a = b = c;
| ^^^^^^^^^
|
= note: `-D clippy::multi-assignments` implied by `-D warnings`

error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:5:5
|
LL | a = b = c = d;
| ^^^^^^^^^^^^^

error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:5:9
|
LL | a = b = c = d;
| ^^^^^^^^^

error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:6:5
|
LL | a = b = { c };
| ^^^^^^^^^^^^^

error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:7:5
|
LL | a = { b = c };
| ^^^^^^^^^^^^^

error: assignments don't nest intuitively
--> $DIR/multi_assignments.rs:8:5
|
LL | a = (b = c);
| ^^^^^^^^^^^

error: aborting due to 6 previous errors