use in_external_macro
This commit is contained in:
parent
1ac30d3c86
commit
14a6fa4a34
@ -253,10 +253,6 @@ 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);
|
||||
@ -296,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;
|
||||
|
@ -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 {
|
||||
() => {
|
||||
|
@ -1,11 +1,15 @@
|
||||
//@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 C: &[u8] = $b.as_bytes();
|
||||
const B: &[u8] = b"warning";
|
||||
};
|
||||
}
|
||||
|
||||
@ -17,7 +21,9 @@ fn str_lit_as_bytes() {
|
||||
let bs = b"lit to string".to_vec();
|
||||
let bs = b"lit to owned".to_vec();
|
||||
|
||||
b!("aaa");
|
||||
b!("warning");
|
||||
|
||||
string_lit_as_bytes!("no warning");
|
||||
|
||||
// no warning, because these cannot be written as byte string literals:
|
||||
let ubs = "☃".as_bytes();
|
||||
|
@ -1,11 +1,15 @@
|
||||
//@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 C: &[u8] = $b.as_bytes();
|
||||
const B: &[u8] = $b.as_bytes();
|
||||
};
|
||||
}
|
||||
|
||||
@ -17,7 +21,9 @@ 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");
|
||||
b!("warning");
|
||||
|
||||
string_lit_as_bytes!("no warning");
|
||||
|
||||
// no warning, because these cannot be written as byte string literals:
|
||||
let ubs = "☃".as_bytes();
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: calling `as_bytes()` on a string literal
|
||||
--> $DIR/string_lit_as_bytes.rs:13: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"`
|
||||
@ -7,34 +7,45 @@ 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:15: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:17: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:18: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:33: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:35: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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user