Skip to content

Don't apply string_lit_as_bytes if in macro expansion #10665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
}

if_chain! {
if !in_external_macro(cx.sess(), e.span);
if let ExprKind::MethodCall(path, receiver, ..) = &e.kind;
if path.ident.name == sym!(as_bytes);
if let ExprKind::Lit(lit) = &receiver.kind;
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ macro_rules! string_add {
};
}

#[macro_export]
macro_rules! string_lit_as_bytes {
($s:literal) => {
const C: &[u8] = $s.as_bytes();
};
}

#[macro_export]
macro_rules! mut_mut {
() => {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/string_lit_as_bytes.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
//@run-rustfix
//@aux-build:macro_rules.rs

#![allow(dead_code, unused_variables)]
#![warn(clippy::string_lit_as_bytes)]

#[macro_use]
extern crate macro_rules;

macro_rules! b {
($b:literal) => {
const B: &[u8] = b"warning";
};
}

fn str_lit_as_bytes() {
let bs = b"hello there";

Expand All @@ -11,6 +21,10 @@ fn str_lit_as_bytes() {
let bs = b"lit to string".to_vec();
let bs = b"lit to owned".to_vec();

b!("warning");

string_lit_as_bytes!("no warning");

// no warning, because these cannot be written as byte string literals:
let ubs = "☃".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/string_lit_as_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
//@run-rustfix
//@aux-build:macro_rules.rs

#![allow(dead_code, unused_variables)]
#![warn(clippy::string_lit_as_bytes)]

#[macro_use]
extern crate macro_rules;

macro_rules! b {
($b:literal) => {
const B: &[u8] = $b.as_bytes();
};
}

fn str_lit_as_bytes() {
let bs = "hello there".as_bytes();

Expand All @@ -11,6 +21,10 @@ fn str_lit_as_bytes() {
let bs = "lit to string".to_string().into_bytes();
let bs = "lit to owned".to_owned().into_bytes();

b!("warning");

string_lit_as_bytes!("no warning");

// no warning, because these cannot be written as byte string literals:
let ubs = "☃".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();
Expand Down
25 changes: 18 additions & 7 deletions tests/ui/string_lit_as_bytes.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
error: calling `as_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:7:14
--> $DIR/string_lit_as_bytes.rs:17:14
|
LL | let bs = "hello there".as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"hello there"`
|
= note: `-D clippy::string-lit-as-bytes` implied by `-D warnings`

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

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

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

error: calling `as_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:12:26
|
LL | const B: &[u8] = $b.as_bytes();
| ^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"warning"`
...
LL | b!("warning");
| ------------- in this macro invocation
|
= note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)

error: calling `as_bytes()` on `include_str!(..)`
--> $DIR/string_lit_as_bytes.rs:25:22
--> $DIR/string_lit_as_bytes.rs:39:22
|
LL | let includestr = include_str!("string_lit_as_bytes.rs").as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("string_lit_as_bytes.rs")`

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

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors