-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abdf173
commit 60e1995
Showing
8 changed files
with
143 additions
and
0 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
Binary file not shown.
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,60 @@ | ||
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, ExprKind, LangItem}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// It detects useless calls to `str::as_bytes()` before calling `len()` or `is_empty()`. | ||
/// | ||
/// ### Why is this bad? | ||
/// The `len()` and `is_empty()` methods are also directly available on strings, and they | ||
/// return identical results. In particular, `len()` on a string returns the number of | ||
/// bytes. | ||
/// | ||
/// ### Example | ||
/// ``` | ||
/// let len = "some string".as_bytes().len(); | ||
/// let b = "some string".as_bytes().is_empty(); | ||
/// ``` | ||
/// Use instead: | ||
/// ``` | ||
/// let len = "some string".len(); | ||
/// let b = "some string".is_empty(); | ||
/// ``` | ||
#[clippy::version = "1.83.0"] | ||
pub NEEDLESS_AS_BYTES, | ||
complexity, | ||
"detect useless calls to `as_bytes()`" | ||
} | ||
|
||
declare_lint_pass!(NeedlessAsBytes => [NEEDLESS_AS_BYTES]); | ||
|
||
impl LateLintPass<'_> for NeedlessAsBytes { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::MethodCall(method2, receiver2, &[], _) = expr.kind | ||
&& cx.typeck_results().expr_ty_adjusted(receiver2).peel_refs().is_slice() | ||
&& (method2.ident.name == sym::len || method2.ident.name.as_str() == "is_empty") | ||
&& let ExprKind::MethodCall(method1, receiver1, &[], _) = receiver2.kind | ||
&& let ty1 = cx.typeck_results().expr_ty_adjusted(receiver1).peel_refs() | ||
&& (is_type_lang_item(cx, ty1, LangItem::String) || ty1.is_str()) | ||
&& method1.ident.name.as_str() == "as_bytes" | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let sugg = Sugg::hir_with_context(cx, receiver1, expr.span.ctxt(), "..", &mut app); | ||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_AS_BYTES, | ||
expr.span, | ||
"needless call to `as_bytes()`", | ||
format!("`{}()` can be called directly on strings", method2.ident.name), | ||
format!("{sugg}.{}()", method2.ident.name), | ||
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,25 @@ | ||
#![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() { | ||
println!("len = {}", "some string".len()); | ||
} | ||
|
||
let s = String::from("yet another string"); | ||
if s.is_empty() { | ||
println!("len = {}", s.len()); | ||
} | ||
|
||
// Do not lint | ||
let _ = S.as_bytes().is_empty(); | ||
let _ = S.as_bytes().len(); | ||
} |
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,25 @@ | ||
#![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() { | ||
println!("len = {}", "some string".as_bytes().len()); | ||
} | ||
|
||
let s = String::from("yet another string"); | ||
if s.as_bytes().is_empty() { | ||
println!("len = {}", s.as_bytes().len()); | ||
} | ||
|
||
// Do not lint | ||
let _ = S.as_bytes().is_empty(); | ||
let _ = S.as_bytes().len(); | ||
} |
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:14: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:18: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:19: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 | ||
|