Skip to content

Commit 55d6914

Browse files
committed
Merge string_add_assign lint into string_add lint
1 parent 08c29d4 commit 55d6914

File tree

9 files changed

+45
-124
lines changed

9 files changed

+45
-124
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,6 @@ Released 2018-09-13
15551555
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
15561556
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string
15571557
[`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add
1558-
[`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign
15591558
[`string_extend_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_extend_chars
15601559
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
15611560
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
780780
&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS,
781781
&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
782782
&strings::STRING_ADD,
783-
&strings::STRING_ADD_ASSIGN,
784783
&strings::STRING_LIT_AS_BYTES,
785784
&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
786785
&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -1147,7 +1146,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11471146
LintId::of(&non_expressive_names::SIMILAR_NAMES),
11481147
LintId::of(&ranges::RANGE_PLUS_ONE),
11491148
LintId::of(&shadow::SHADOW_UNRELATED),
1150-
LintId::of(&strings::STRING_ADD_ASSIGN),
11511149
LintId::of(&trait_bounds::TYPE_REPETITION_IN_BOUNDS),
11521150
LintId::of(&trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF),
11531151
LintId::of(&types::CAST_LOSSLESS),

clippy_lints/src/strings.rs

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,14 @@ use rustc_span::source_map::Spanned;
88
use if_chain::if_chain;
99

1010
use crate::utils::SpanlessEq;
11-
use crate::utils::{get_parent_expr, is_allowed, is_type_diagnostic_item, span_lint, span_lint_and_sugg, walk_ptrs_ty};
11+
use crate::utils::{get_parent_expr, is_type_diagnostic_item, span_lint, span_lint_and_sugg, walk_ptrs_ty};
1212

1313
declare_clippy_lint! {
14-
/// **What it does:** Checks for string appends of the form `x = x + y` (without
15-
/// `let`!).
14+
/// **What it does:** Checks for all instances of `x + _` or `x = x + y` (without `let`)
15+
/// where `x` is of type `String`.
1616
///
17-
/// **Why is this bad?** It's not really bad, but some people think that the
18-
/// `.push_str(_)` method is more readable.
19-
///
20-
/// **Known problems:** None.
21-
///
22-
/// **Example:**
23-
///
24-
/// ```rust
25-
/// let mut x = "Hello".to_owned();
26-
/// x = x + ", World";
27-
/// ```
28-
pub STRING_ADD_ASSIGN,
29-
pedantic,
30-
"using `x = x + ..` where x is a `String` instead of `push_str()`"
31-
}
32-
33-
declare_clippy_lint! {
34-
/// **What it does:** Checks for all instances of `x + _` where `x` is of type
35-
/// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
36-
/// match.
37-
///
38-
/// **Why is this bad?** It's not bad in and of itself. However, this particular
39-
/// `Add` implementation is asymmetric (the other operand need not be `String`,
17+
/// **Why is this bad?** It's not bad in and of itself, but `String::push_str` is more readable.
18+
/// However, this particular `Add` implementation is asymmetric (the other operand need not be `String`,
4019
/// but `x` does), while addition as mathematically defined is symmetric, also
4120
/// the `String::push_str(_)` function is a perfectly good replacement.
4221
/// Therefore, some dislike it and wish not to have it in their code.
@@ -47,11 +26,27 @@ declare_clippy_lint! {
4726
///
4827
/// **Known problems:** None.
4928
///
50-
/// **Example:**
29+
/// **Examples:**
30+
/// ```rust,ignore
31+
/// # let x = "Hello".to_owned();
5132
///
52-
/// ```rust
53-
/// let x = "Hello".to_owned();
33+
/// // Bad
5434
/// x + ", World";
35+
///
36+
/// // Good
37+
/// x.push_str(", World");
38+
/// ```
39+
///
40+
/// // or
41+
///
42+
/// ```rust,ignore
43+
/// # let mut x = "Hello".to_owned();
44+
///
45+
/// // Bad
46+
/// x = x + ", World";
47+
///
48+
/// // Good
49+
/// x += ", World";
5550
/// ```
5651
pub STRING_ADD,
5752
restriction,
@@ -69,14 +64,19 @@ declare_clippy_lint! {
6964
///
7065
/// **Example:**
7166
/// ```rust
67+
///
68+
/// // Bad
7269
/// let bs = "a byte string".as_bytes();
70+
///
71+
/// // Good
72+
/// let bs = b"a byte string";
7373
/// ```
7474
pub STRING_LIT_AS_BYTES,
7575
style,
7676
"calling `as_bytes` on a string literal instead of using a byte string literal"
7777
}
7878

79-
declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);
79+
declare_lint_pass!(StringAdd => [STRING_ADD]);
8080

8181
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
8282
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
@@ -93,14 +93,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
9393
) = e.kind
9494
{
9595
if is_string(cx, left) {
96-
if !is_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) {
97-
let parent = get_parent_expr(cx, e);
98-
if let Some(p) = parent {
99-
if let ExprKind::Assign(ref target, _, _) = p.kind {
100-
// avoid duplicate matches
101-
if SpanlessEq::new(cx).eq_expr(target, left) {
102-
return;
103-
}
96+
if let Some(parent) = get_parent_expr(cx, e) {
97+
if let ExprKind::Assign(ref target, _, _) = parent.kind {
98+
// avoid duplicate matches
99+
if SpanlessEq::new(cx).eq_expr(target, left) {
100+
return;
104101
}
105102
}
106103
}
@@ -115,10 +112,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
115112
if is_string(cx, target) && is_add(cx, src, target) {
116113
span_lint(
117114
cx,
118-
STRING_ADD_ASSIGN,
115+
STRING_ADD,
119116
e.span,
120117
"you assigned the result of adding something to this string. Consider using \
121-
`String::push_str()` instead",
118+
`String::push_str()` instead",
122119
);
123120
}
124121
}

src/lintlist/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,13 +1977,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
19771977
deprecation: None,
19781978
module: "strings",
19791979
},
1980-
Lint {
1981-
name: "string_add_assign",
1982-
group: "pedantic",
1983-
desc: "using `x = x + ..` where x is a `String` instead of `push_str()`",
1984-
deprecation: None,
1985-
module: "strings",
1986-
},
19871980
Lint {
19881981
name: "string_extend_chars",
19891982
group: "style",

tests/ui/string_add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
extern crate macro_rules;
55

66
#[warn(clippy::string_add)]
7-
#[allow(clippy::string_add_assign, unused)]
7+
#[allow(unused)]
88
fn main() {
99
// ignores assignment distinction
1010
let mut x = "".to_owned();

tests/ui/string_add.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: manual implementation of an assign operation
1+
error: you assigned the result of adding something to this string. Consider using `String::push_str()` instead
22
--> $DIR/string_add.rs:13:9
33
|
44
LL | x = x + ".";
5-
| ^^^^^^^^^^^ help: replace it with: `x += "."`
5+
| ^^^^^^^^^^^
66
|
7-
= note: `-D clippy::assign-op-pattern` implied by `-D warnings`
7+
= note: `-D clippy::string-add` implied by `-D warnings`
88

9-
error: you added something to a string. Consider using `String::push_str()` instead
10-
--> $DIR/string_add.rs:13:13
9+
error: manual implementation of an assign operation
10+
--> $DIR/string_add.rs:13:9
1111
|
1212
LL | x = x + ".";
13-
| ^^^^^^^
13+
| ^^^^^^^^^^^ help: replace it with: `x += "."`
1414
|
15-
= note: `-D clippy::string-add` implied by `-D warnings`
15+
= note: `-D clippy::assign-op-pattern` implied by `-D warnings`
1616

1717
error: you added something to a string. Consider using `String::push_str()` instead
1818
--> $DIR/string_add.rs:17:13

tests/ui/string_add_assign.fixed

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/ui/string_add_assign.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/ui/string_add_assign.stderr

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)