Skip to content

Commit ff1607e

Browse files
committed
Auto merge of #4880 - daxpedda:string-add, r=phansch
Fix false positive in `string_add`. `clippy::string_add` was popping up in macros. I'm not sure what clippy's general direction is in these matters, but I can change it to be external macros only too. --- changelog: Fix false positives for `string_add` in macro expansions.
2 parents 374db5c + 946961d commit ff1607e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

clippy_lints/src/strings.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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;
@@ -80,6 +80,10 @@ declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);
8080

8181
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
8282
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
83+
if in_external_macro(cx.sess(), e.span) {
84+
return;
85+
}
86+
8387
if let ExprKind::Binary(
8488
Spanned {
8589
node: BinOpKind::Add, ..

tests/ui/auxiliary/macro_rules.rs

+8
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

+7
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() {
@@ -16,4 +21,6 @@ fn main() {
1621
let mut x = 1;
1722
x = x + 1;
1823
assert_eq!(2, x);
24+
25+
string_add!();
1926
}

tests/ui/string_add.stderr

+4-4
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)