Skip to content

Commit e42e354

Browse files
committed
Remove needless check of returned type
We are checking that we are calling the `as_bytes()` method of `String` or `str`. Checking that it returns a `slice()` does not add anything.
1 parent 19e305b commit e42e354

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,7 +4925,7 @@ impl Methods {
49254925
("is_empty", []) => {
49264926
match method_call(recv) {
49274927
Some(("as_bytes", prev_recv, [], _, _)) => {
4928-
needless_as_bytes::check(cx, "is_empty", recv, prev_recv, expr.span);
4928+
needless_as_bytes::check(cx, "is_empty", prev_recv, expr.span);
49294929
},
49304930
Some(("as_str", recv, [], as_str_span, _)) => {
49314931
redundant_as_str::check(cx, expr, recv, as_str_span, span);
@@ -4963,7 +4963,7 @@ impl Methods {
49634963
},
49644964
("len", []) => {
49654965
if let Some(("as_bytes", prev_recv, [], _, _)) = method_call(recv) {
4966-
needless_as_bytes::check(cx, "len", recv, prev_recv, expr.span);
4966+
needless_as_bytes::check(cx, "len", prev_recv, expr.span);
49674967
}
49684968
},
49694969
("lock", []) => {

clippy_lints/src/methods/needless_as_bytes.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ use rustc_span::Span;
88

99
use super::NEEDLESS_AS_BYTES;
1010

11-
pub fn check(cx: &LateContext<'_>, method: &str, recv: &Expr<'_>, prev_recv: &Expr<'_>, span: Span) {
12-
if cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice()
13-
&& let ty1 = cx.typeck_results().expr_ty_adjusted(prev_recv).peel_refs()
14-
&& (is_type_lang_item(cx, ty1, LangItem::String) || ty1.is_str())
15-
{
11+
pub fn check(cx: &LateContext<'_>, method: &str, prev_recv: &Expr<'_>, span: Span) {
12+
let ty1 = cx.typeck_results().expr_ty_adjusted(prev_recv).peel_refs();
13+
if is_type_lang_item(cx, ty1, LangItem::String) || ty1.is_str() {
1614
let mut app = Applicability::MachineApplicable;
1715
let sugg = Sugg::hir_with_context(cx, prev_recv, span.ctxt(), "..", &mut app);
1816
span_lint_and_sugg(

0 commit comments

Comments
 (0)