Skip to content

Commit 160b41d

Browse files
deprecate assign_ops lint
1 parent 99a087b commit 160b41d

File tree

5 files changed

+46
-164
lines changed

5 files changed

+46
-164
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,6 @@ use rustc::{declare_lint, lint_array};
77
use if_chain::if_chain;
88
use syntax::ast;
99

10-
/// **What it does:** Checks for compound assignment operations (`+=` and
11-
/// similar).
12-
///
13-
/// **Why is this bad?** Projects with many developers from languages without
14-
/// those operations may find them unreadable and not worth their weight.
15-
///
16-
/// **Known problems:** Types implementing `OpAssign` don't necessarily
17-
/// implement `Op`.
18-
///
19-
/// **Example:**
20-
/// ```rust
21-
/// a += 1;
22-
/// ```
23-
declare_clippy_lint! {
24-
pub ASSIGN_OPS,
25-
restriction,
26-
"any compound assignment operation"
27-
}
28-
2910
/// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
3011
/// patterns.
3112
///
@@ -73,24 +54,14 @@ pub struct AssignOps;
7354

7455
impl LintPass for AssignOps {
7556
fn get_lints(&self) -> LintArray {
76-
lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
57+
lint_array!(ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
7758
}
7859
}
7960

8061
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
8162
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
8263
match expr.node {
8364
hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
84-
span_lint_and_then(cx, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
85-
let lhs = &sugg::Sugg::hir(cx, lhs, "..");
86-
let rhs = &sugg::Sugg::hir(cx, rhs, "..");
87-
88-
db.span_suggestion(
89-
expr.span,
90-
"replace it with",
91-
format!("{} = {}", lhs, sugg::make_binop(higher::binop(op.node), lhs, rhs)),
92-
);
93-
});
9465
if let hir::ExprKind::Binary(binop, ref l, ref r) = rhs.node {
9566
if op.node == binop.node {
9667
let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {

clippy_lints/src/deprecated_lints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,13 @@ declare_deprecated_lint! {
8282
pub MISALIGNED_TRANSMUTE,
8383
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr"
8484
}
85+
86+
/// **What it does:** Nothing. This lint has been deprecated.
87+
///
88+
/// **Deprecation reason:** This lint is too subjective, not having a good reason for being in clippy.
89+
/// Additionally, compound assignment operators may be overloaded separately from their non-assigning
90+
/// counterparts, so this lint may suggest a change in behavior or the code may not compile.
91+
declare_deprecated_lint! {
92+
pub ASSIGN_OPS,
93+
"using compound assignment operators (e.g. `+=`) is harmless"
94+
}

clippy_lints/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
261261
"misaligned_transmute",
262262
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",
263263
);
264+
store.register_removed(
265+
"assign_ops",
266+
"using compound assignment operators (e.g. `+=`) is harmless",
267+
);
264268
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
265269

266270
reg.register_late_lint_pass(box serde_api::Serde);
@@ -406,7 +410,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
406410
reg.register_lint_group("clippy_restriction", vec![
407411
arithmetic::FLOAT_ARITHMETIC,
408412
arithmetic::INTEGER_ARITHMETIC,
409-
assign_ops::ASSIGN_OPS,
410413
else_if_without_else::ELSE_IF_WITHOUT_ELSE,
411414
indexing_slicing::INDEXING_SLICING,
412415
inherent_impl::MULTIPLE_INHERENT_IMPL,

tests/ui/assign_ops.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
1-
2-
3-
4-
#[warn(assign_ops)]
5-
#[allow(unused_assignments)]
6-
fn main() {
7-
let mut i = 1i32;
8-
i += 2;
9-
i += 2 + 17;
10-
i -= 6;
11-
i -= 2 - 1;
12-
i *= 5;
13-
i *= 1+5;
14-
i /= 32;
15-
i /= 32 | 5;
16-
i /= 32 / 5;
17-
i %= 42;
18-
i >>= i;
19-
i <<= 9 + 6 - 7;
20-
i += 1 << 5;
21-
}
22-
231
#[allow(dead_code, unused_assignments)]
242
#[warn(assign_op_pattern)]
25-
fn bla() {
3+
fn main() {
264
let mut a = 5;
275
a = a + 1;
286
a = 1 + a;

tests/ui/assign_ops.stderr

Lines changed: 30 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,58 @@
1-
error: assign operation detected
2-
--> $DIR/assign_ops.rs:8:5
1+
error: manual implementation of an assign operation
2+
--> $DIR/assign_ops.rs:5:5
33
|
4-
8 | i += 2;
5-
| ^^^^^^ help: replace it with: `i = i + 2`
4+
5 | a = a + 1;
5+
| ^^^^^^^^^ help: replace it with: `a += 1`
66
|
7-
= note: `-D assign-ops` implied by `-D warnings`
8-
9-
error: assign operation detected
10-
--> $DIR/assign_ops.rs:9:5
11-
|
12-
9 | i += 2 + 17;
13-
| ^^^^^^^^^^^ help: replace it with: `i = i + 2 + 17`
14-
15-
error: assign operation detected
16-
--> $DIR/assign_ops.rs:10:5
17-
|
18-
10 | i -= 6;
19-
| ^^^^^^ help: replace it with: `i = i - 6`
20-
21-
error: assign operation detected
22-
--> $DIR/assign_ops.rs:11:5
23-
|
24-
11 | i -= 2 - 1;
25-
| ^^^^^^^^^^ help: replace it with: `i = i - (2 - 1)`
26-
27-
error: assign operation detected
28-
--> $DIR/assign_ops.rs:12:5
29-
|
30-
12 | i *= 5;
31-
| ^^^^^^ help: replace it with: `i = i * 5`
32-
33-
error: assign operation detected
34-
--> $DIR/assign_ops.rs:13:5
35-
|
36-
13 | i *= 1+5;
37-
| ^^^^^^^^ help: replace it with: `i = i * (1+5)`
38-
39-
error: assign operation detected
40-
--> $DIR/assign_ops.rs:14:5
41-
|
42-
14 | i /= 32;
43-
| ^^^^^^^ help: replace it with: `i = i / 32`
44-
45-
error: assign operation detected
46-
--> $DIR/assign_ops.rs:15:5
47-
|
48-
15 | i /= 32 | 5;
49-
| ^^^^^^^^^^^ help: replace it with: `i = i / (32 | 5)`
50-
51-
error: assign operation detected
52-
--> $DIR/assign_ops.rs:16:5
53-
|
54-
16 | i /= 32 / 5;
55-
| ^^^^^^^^^^^ help: replace it with: `i = i / (32 / 5)`
56-
57-
error: assign operation detected
58-
--> $DIR/assign_ops.rs:17:5
59-
|
60-
17 | i %= 42;
61-
| ^^^^^^^ help: replace it with: `i = i % 42`
62-
63-
error: assign operation detected
64-
--> $DIR/assign_ops.rs:18:5
65-
|
66-
18 | i >>= i;
67-
| ^^^^^^^ help: replace it with: `i = i >> i`
68-
69-
error: assign operation detected
70-
--> $DIR/assign_ops.rs:19:5
71-
|
72-
19 | i <<= 9 + 6 - 7;
73-
| ^^^^^^^^^^^^^^^ help: replace it with: `i = i << (9 + 6 - 7)`
74-
75-
error: assign operation detected
76-
--> $DIR/assign_ops.rs:20:5
77-
|
78-
20 | i += 1 << 5;
79-
| ^^^^^^^^^^^ help: replace it with: `i = i + (1 << 5)`
7+
= note: `-D assign-op-pattern` implied by `-D warnings`
808

819
error: manual implementation of an assign operation
82-
--> $DIR/assign_ops.rs:27:5
83-
|
84-
27 | a = a + 1;
85-
| ^^^^^^^^^ help: replace it with: `a += 1`
86-
|
87-
= note: `-D assign-op-pattern` implied by `-D warnings`
88-
89-
error: manual implementation of an assign operation
90-
--> $DIR/assign_ops.rs:28:5
91-
|
92-
28 | a = 1 + a;
93-
| ^^^^^^^^^ help: replace it with: `a += 1`
10+
--> $DIR/assign_ops.rs:6:5
11+
|
12+
6 | a = 1 + a;
13+
| ^^^^^^^^^ help: replace it with: `a += 1`
9414

9515
error: manual implementation of an assign operation
96-
--> $DIR/assign_ops.rs:29:5
97-
|
98-
29 | a = a - 1;
99-
| ^^^^^^^^^ help: replace it with: `a -= 1`
16+
--> $DIR/assign_ops.rs:7:5
17+
|
18+
7 | a = a - 1;
19+
| ^^^^^^^^^ help: replace it with: `a -= 1`
10020

10121
error: manual implementation of an assign operation
102-
--> $DIR/assign_ops.rs:30:5
103-
|
104-
30 | a = a * 99;
105-
| ^^^^^^^^^^ help: replace it with: `a *= 99`
22+
--> $DIR/assign_ops.rs:8:5
23+
|
24+
8 | a = a * 99;
25+
| ^^^^^^^^^^ help: replace it with: `a *= 99`
10626

10727
error: manual implementation of an assign operation
108-
--> $DIR/assign_ops.rs:31:5
109-
|
110-
31 | a = 42 * a;
111-
| ^^^^^^^^^^ help: replace it with: `a *= 42`
28+
--> $DIR/assign_ops.rs:9:5
29+
|
30+
9 | a = 42 * a;
31+
| ^^^^^^^^^^ help: replace it with: `a *= 42`
11232

11333
error: manual implementation of an assign operation
114-
--> $DIR/assign_ops.rs:32:5
34+
--> $DIR/assign_ops.rs:10:5
11535
|
116-
32 | a = a / 2;
36+
10 | a = a / 2;
11737
| ^^^^^^^^^ help: replace it with: `a /= 2`
11838

11939
error: manual implementation of an assign operation
120-
--> $DIR/assign_ops.rs:33:5
40+
--> $DIR/assign_ops.rs:11:5
12141
|
122-
33 | a = a % 5;
42+
11 | a = a % 5;
12343
| ^^^^^^^^^ help: replace it with: `a %= 5`
12444

12545
error: manual implementation of an assign operation
126-
--> $DIR/assign_ops.rs:34:5
46+
--> $DIR/assign_ops.rs:12:5
12747
|
128-
34 | a = a & 1;
48+
12 | a = a & 1;
12949
| ^^^^^^^^^ help: replace it with: `a &= 1`
13050

13151
error: manual implementation of an assign operation
132-
--> $DIR/assign_ops.rs:40:5
52+
--> $DIR/assign_ops.rs:18:5
13353
|
134-
40 | s = s + "bla";
54+
18 | s = s + "bla";
13555
| ^^^^^^^^^^^^^ help: replace it with: `s += "bla"`
13656

137-
error: aborting due to 22 previous errors
57+
error: aborting due to 9 previous errors
13858

0 commit comments

Comments
 (0)