mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 07:04:18 +00:00
don't check if from macro invocation
This commit is contained in:
parent
c4f2c48d9b
commit
acfb2c45ba
4 changed files with 26 additions and 6 deletions
|
@ -253,6 +253,10 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
|
|||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
||||
use rustc_ast::LitKind;
|
||||
|
||||
if e.span.from_expansion() {
|
||||
return;
|
||||
}
|
||||
|
||||
if_chain! {
|
||||
// Find std::str::converts::from_utf8
|
||||
if let Some(args) = match_function_call(cx, e, &paths::STR_FROM_UTF8);
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
#![allow(dead_code, unused_variables)]
|
||||
#![warn(clippy::string_lit_as_bytes)]
|
||||
|
||||
macro_rules! b {
|
||||
($b:literal) => {
|
||||
const C: &[u8] = $b.as_bytes();
|
||||
}
|
||||
}
|
||||
|
||||
fn str_lit_as_bytes() {
|
||||
let bs = b"hello there";
|
||||
|
||||
|
@ -11,6 +17,8 @@ fn str_lit_as_bytes() {
|
|||
let bs = b"lit to string".to_vec();
|
||||
let bs = b"lit to owned".to_vec();
|
||||
|
||||
b!("aaa");
|
||||
|
||||
// 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();
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
#![allow(dead_code, unused_variables)]
|
||||
#![warn(clippy::string_lit_as_bytes)]
|
||||
|
||||
macro_rules! b {
|
||||
($b:literal) => {
|
||||
const C: &[u8] = $b.as_bytes();
|
||||
}
|
||||
}
|
||||
|
||||
fn str_lit_as_bytes() {
|
||||
let bs = "hello there".as_bytes();
|
||||
|
||||
|
@ -11,6 +17,8 @@ fn str_lit_as_bytes() {
|
|||
let bs = "lit to string".to_string().into_bytes();
|
||||
let bs = "lit to owned".to_owned().into_bytes();
|
||||
|
||||
b!("aaa");
|
||||
|
||||
// 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();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
error: calling `as_bytes()` on a string literal
|
||||
--> $DIR/string_lit_as_bytes.rs:7:14
|
||||
--> $DIR/string_lit_as_bytes.rs:13:14
|
||||
|
|
||||
LL | let bs = "hello there".as_bytes();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"hello there"`
|
||||
|
@ -7,31 +7,31 @@ LL | let bs = "hello there".as_bytes();
|
|||
= 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:15: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:17: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:18: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 `include_str!(..)`
|
||||
--> $DIR/string_lit_as_bytes.rs:25:22
|
||||
--> $DIR/string_lit_as_bytes.rs:33: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:35:13
|
||||
|
|
||||
LL | let _ = "string with newline/t/n".as_bytes();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"string with newline/t/n"`
|
||||
|
|
Loading…
Reference in a new issue