-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #13437 - samueltardieu:issue-13434, r=y21
New lint `needless_as_bytes` changelog: [`needless_as_bytes`]: new lint Fix #13434
- Loading branch information
Showing
9 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::sugg::Sugg; | ||
use clippy_utils::ty::is_type_lang_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, LangItem}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
use super::NEEDLESS_AS_BYTES; | ||
|
||
pub fn check(cx: &LateContext<'_>, method: &str, recv: &Expr<'_>, prev_recv: &Expr<'_>, span: Span) { | ||
if cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice() | ||
&& let ty1 = cx.typeck_results().expr_ty_adjusted(prev_recv).peel_refs() | ||
&& (is_type_lang_item(cx, ty1, LangItem::String) || ty1.is_str()) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let sugg = Sugg::hir_with_context(cx, prev_recv, span.ctxt(), "..", &mut app); | ||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_AS_BYTES, | ||
span, | ||
"needless call to `as_bytes()`", | ||
format!("`{method}()` can be called directly on strings"), | ||
format!("{sugg}.{method}()"), | ||
app, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#![warn(clippy::needless_as_bytes)] | ||
#![allow(clippy::const_is_empty)] | ||
|
||
struct S; | ||
|
||
impl S { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[] | ||
} | ||
} | ||
|
||
fn main() { | ||
if "some string".is_empty() { | ||
//~^ needless_as_bytes | ||
println!("len = {}", "some string".len()); | ||
//~^ needless_as_bytes | ||
} | ||
|
||
let s = String::from("yet another string"); | ||
if s.is_empty() { | ||
//~^ needless_as_bytes | ||
println!("len = {}", s.len()); | ||
//~^ needless_as_bytes | ||
} | ||
|
||
// Do not lint | ||
let _ = S.as_bytes().is_empty(); | ||
let _ = S.as_bytes().len(); | ||
let _ = (&String::new() as &dyn AsBytes).as_bytes().len(); | ||
macro_rules! m { | ||
(1) => { | ||
"" | ||
}; | ||
(2) => { | ||
"".as_bytes() | ||
}; | ||
} | ||
m!(1).as_bytes().len(); | ||
m!(2).len(); | ||
} | ||
|
||
pub trait AsBytes { | ||
fn as_bytes(&self) -> &[u8]; | ||
} | ||
|
||
impl AsBytes for String { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#![warn(clippy::needless_as_bytes)] | ||
#![allow(clippy::const_is_empty)] | ||
|
||
struct S; | ||
|
||
impl S { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[] | ||
} | ||
} | ||
|
||
fn main() { | ||
if "some string".as_bytes().is_empty() { | ||
//~^ needless_as_bytes | ||
println!("len = {}", "some string".as_bytes().len()); | ||
//~^ needless_as_bytes | ||
} | ||
|
||
let s = String::from("yet another string"); | ||
if s.as_bytes().is_empty() { | ||
//~^ needless_as_bytes | ||
println!("len = {}", s.as_bytes().len()); | ||
//~^ needless_as_bytes | ||
} | ||
|
||
// Do not lint | ||
let _ = S.as_bytes().is_empty(); | ||
let _ = S.as_bytes().len(); | ||
let _ = (&String::new() as &dyn AsBytes).as_bytes().len(); | ||
macro_rules! m { | ||
(1) => { | ||
"" | ||
}; | ||
(2) => { | ||
"".as_bytes() | ||
}; | ||
} | ||
m!(1).as_bytes().len(); | ||
m!(2).len(); | ||
} | ||
|
||
pub trait AsBytes { | ||
fn as_bytes(&self) -> &[u8]; | ||
} | ||
|
||
impl AsBytes for String { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error: needless call to `as_bytes()` | ||
--> tests/ui/needless_as_bytes.rs:13:8 | ||
| | ||
LL | if "some string".as_bytes().is_empty() { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `is_empty()` can be called directly on strings: `"some string".is_empty()` | ||
| | ||
= note: `-D clippy::needless-as-bytes` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::needless_as_bytes)]` | ||
|
||
error: needless call to `as_bytes()` | ||
--> tests/ui/needless_as_bytes.rs:15:30 | ||
| | ||
LL | println!("len = {}", "some string".as_bytes().len()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `len()` can be called directly on strings: `"some string".len()` | ||
|
||
error: needless call to `as_bytes()` | ||
--> tests/ui/needless_as_bytes.rs:20:8 | ||
| | ||
LL | if s.as_bytes().is_empty() { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: `is_empty()` can be called directly on strings: `s.is_empty()` | ||
|
||
error: needless call to `as_bytes()` | ||
--> tests/ui/needless_as_bytes.rs:22:30 | ||
| | ||
LL | println!("len = {}", s.as_bytes().len()); | ||
| ^^^^^^^^^^^^^^^^^^ help: `len()` can be called directly on strings: `s.len()` | ||
|
||
error: aborting due to 4 previous errors | ||
|