Skip to content

Commit acfb2c4

Browse files
committed
don't check if from macro invocation
1 parent c4f2c48 commit acfb2c4

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

clippy_lints/src/strings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
253253
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
254254
use rustc_ast::LitKind;
255255

256+
if e.span.from_expansion() {
257+
return;
258+
}
259+
256260
if_chain! {
257261
// Find std::str::converts::from_utf8
258262
if let Some(args) = match_function_call(cx, e, &paths::STR_FROM_UTF8);

tests/ui/string_lit_as_bytes.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#![allow(dead_code, unused_variables)]
44
#![warn(clippy::string_lit_as_bytes)]
55

6+
macro_rules! b {
7+
($b:literal) => {
8+
const C: &[u8] = $b.as_bytes();
9+
}
10+
}
11+
612
fn str_lit_as_bytes() {
713
let bs = b"hello there";
814

@@ -11,6 +17,8 @@ fn str_lit_as_bytes() {
1117
let bs = b"lit to string".to_vec();
1218
let bs = b"lit to owned".to_vec();
1319

20+
b!("aaa");
21+
1422
// no warning, because these cannot be written as byte string literals:
1523
let ubs = "☃".as_bytes();
1624
let ubs = "hello there! this is a very long string".as_bytes();

tests/ui/string_lit_as_bytes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#![allow(dead_code, unused_variables)]
44
#![warn(clippy::string_lit_as_bytes)]
55

6+
macro_rules! b {
7+
($b:literal) => {
8+
const C: &[u8] = $b.as_bytes();
9+
}
10+
}
11+
612
fn str_lit_as_bytes() {
713
let bs = "hello there".as_bytes();
814

@@ -11,6 +17,8 @@ fn str_lit_as_bytes() {
1117
let bs = "lit to string".to_string().into_bytes();
1218
let bs = "lit to owned".to_owned().into_bytes();
1319

20+
b!("aaa");
21+
1422
// no warning, because these cannot be written as byte string literals:
1523
let ubs = "☃".as_bytes();
1624
let ubs = "hello there! this is a very long string".as_bytes();

tests/ui/string_lit_as_bytes.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error: calling `as_bytes()` on a string literal
2-
--> $DIR/string_lit_as_bytes.rs:7:14
2+
--> $DIR/string_lit_as_bytes.rs:13:14
33
|
44
LL | let bs = "hello there".as_bytes();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"hello there"`
66
|
77
= note: `-D clippy::string-lit-as-bytes` implied by `-D warnings`
88

99
error: calling `as_bytes()` on a string literal
10-
--> $DIR/string_lit_as_bytes.rs:9:14
10+
--> $DIR/string_lit_as_bytes.rs:15:14
1111
|
1212
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`
1414

1515
error: calling `into_bytes()` on a string literal
16-
--> $DIR/string_lit_as_bytes.rs:11:14
16+
--> $DIR/string_lit_as_bytes.rs:17:14
1717
|
1818
LL | let bs = "lit to string".to_string().into_bytes();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to string".to_vec()`
2020

2121
error: calling `into_bytes()` on a string literal
22-
--> $DIR/string_lit_as_bytes.rs:12:14
22+
--> $DIR/string_lit_as_bytes.rs:18:14
2323
|
2424
LL | let bs = "lit to owned".to_owned().into_bytes();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to owned".to_vec()`
2626

2727
error: calling `as_bytes()` on `include_str!(..)`
28-
--> $DIR/string_lit_as_bytes.rs:25:22
28+
--> $DIR/string_lit_as_bytes.rs:33:22
2929
|
3030
LL | let includestr = include_str!("string_lit_as_bytes.rs").as_bytes();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("string_lit_as_bytes.rs")`
3232

3333
error: calling `as_bytes()` on a string literal
34-
--> $DIR/string_lit_as_bytes.rs:27:13
34+
--> $DIR/string_lit_as_bytes.rs:35:13
3535
|
3636
LL | let _ = "string with newline/t/n".as_bytes();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"string with newline/t/n"`

0 commit comments

Comments
 (0)