-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint needless_as_bytes
#13437
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
New lint needless_as_bytes
#13437
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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() { | ||
samueltardieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ 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() { | ||
samueltardieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ 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 hidden or 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.