Skip to content

Commit 946961d

Browse files
committed
Change to only detect in external macros.
1 parent d11b958 commit 946961d

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

clippy_lints/src/strings.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use rustc::declare_lint_pass;
22
use rustc::hir::*;
3-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
44
use rustc_errors::Applicability;
55
use rustc_session::declare_tool_lint;
66
use syntax::source_map::Spanned;
77

88
use if_chain::if_chain;
99

1010
use crate::utils::SpanlessEq;
11-
use crate::utils::{
12-
get_parent_expr, in_macro, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty,
13-
};
11+
use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty};
1412

1513
declare_clippy_lint! {
1614
/// **What it does:** Checks for string appends of the form `x = x + y` (without
@@ -82,7 +80,7 @@ declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);
8280

8381
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
8482
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
85-
if in_macro(e.span) {
83+
if in_external_macro(cx.sess(), e.span) {
8684
return;
8785
}
8886

tests/ui/auxiliary/macro_rules.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ macro_rules! try_err {
3131
}
3232
};
3333
}
34+
35+
#[macro_export]
36+
macro_rules! string_add {
37+
() => {
38+
let y = "".to_owned();
39+
let z = y + "...";
40+
};
41+
}

tests/ui/string_add.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// aux-build:macro_rules.rs
2+
3+
#[macro_use]
4+
extern crate macro_rules;
5+
16
#[warn(clippy::string_add)]
27
#[allow(clippy::string_add_assign, unused)]
38
fn main() {
@@ -17,12 +22,5 @@ fn main() {
1722
x = x + 1;
1823
assert_eq!(2, x);
1924

20-
macro_rules! mac {
21-
() => {
22-
let y = "".to_owned();
23-
let z = y + "...";
24-
};
25-
}
26-
27-
mac!();
25+
string_add!();
2826
}

tests/ui/string_add.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
error: manual implementation of an assign operation
2-
--> $DIR/string_add.rs:8:9
2+
--> $DIR/string_add.rs:13:9
33
|
44
LL | x = x + ".";
55
| ^^^^^^^^^^^ help: replace it with: `x += "."`
66
|
77
= note: `-D clippy::assign-op-pattern` implied by `-D warnings`
88

99
error: you added something to a string. Consider using `String::push_str()` instead
10-
--> $DIR/string_add.rs:8:13
10+
--> $DIR/string_add.rs:13:13
1111
|
1212
LL | x = x + ".";
1313
| ^^^^^^^
1414
|
1515
= note: `-D clippy::string-add` implied by `-D warnings`
1616

1717
error: you added something to a string. Consider using `String::push_str()` instead
18-
--> $DIR/string_add.rs:12:13
18+
--> $DIR/string_add.rs:17:13
1919
|
2020
LL | let z = y + "...";
2121
| ^^^^^^^^^
2222

2323
error: manual implementation of an assign operation
24-
--> $DIR/string_add.rs:17:5
24+
--> $DIR/string_add.rs:22:5
2525
|
2626
LL | x = x + 1;
2727
| ^^^^^^^^^ help: replace it with: `x += 1`

0 commit comments

Comments
 (0)