Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions clippy_lints/src/methods/useless_asref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{implements_trait, peel_and_count_ty_refs, should_call_clone_as_function};
use clippy_utils::{get_parent_expr, peel_blocks, strip_pat_refs};
use rustc_errors::Applicability;
use rustc_hir::{self as hir, LangItem};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{self as hir, LangItem, Node};
use rustc_lint::LateContext;
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::{Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
Expand Down Expand Up @@ -69,14 +70,37 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: Symbo
}
}

// Add `*` derefs if the expr is used in a ctor, because automatic derefs don't apply in that case.
let deref = if rcv_depth > res_depth {
let parent = cx.tcx.parent_hir_node(expr.hir_id);
match parent {
Node::ExprField(_) => "*".repeat(rcv_depth - res_depth),
Node::Expr(parent)
if let hir::ExprKind::Call(func, _) = parent.kind
&& let (_, Some(path)) = func.opt_res_path()
&& matches!(path.res, Res::Def(DefKind::Ctor(_, _), _) | Res::SelfCtor(_)) =>
{
"*".repeat(rcv_depth - res_depth)
},
_ => String::new(),
}
} else {
String::new()
};

let mut applicability = Applicability::MachineApplicable;
let suggestion = format!(
"{deref}{}",
snippet_with_applicability(cx, recvr.span, "..", &mut applicability)
);

span_lint_and_sugg(
cx,
USELESS_ASREF,
expr.span,
format!("this call to `{call_name}` does nothing"),
"try",
snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
suggestion,
applicability,
);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/useless_asref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,41 @@ fn issue_14828() {
().as_ref();
}

fn issue16098(exts: Vec<&str>) {
use std::borrow::Cow;

let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(*s)).collect();
//~^ useless_asref

trait Identity {
fn id(self) -> Self
where
Self: Sized,
{
self
}
}
impl Identity for &str {}

let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(s.id())).collect();
//~^ useless_asref

let v: Vec<Cow<'_, str>> = exts
.iter()
.map(|s| Cow::Borrowed(*std::convert::identity(s)))
//~^ useless_asref
.collect();

struct Wrapper<'a>(&'a str);
let exts_field: Vec<Wrapper> = exts.iter().map(|s| Wrapper(s)).collect();
let v: Vec<Cow<'_, str>> = exts_field.iter().map(|w| Cow::Borrowed(w.0)).collect();
//~^ useless_asref

let exts_index: Vec<&[&str]> = exts.iter().map(|s| std::slice::from_ref(s)).collect();
let v: Vec<Cow<'_, str>> = exts_index.iter().map(|arr| Cow::Borrowed(arr[0])).collect();
//~^ useless_asref
}

fn main() {
not_ok();
ok();
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/useless_asref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,41 @@ fn issue_14828() {
().as_ref();
}

fn issue16098(exts: Vec<&str>) {
use std::borrow::Cow;

let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(s.as_ref())).collect();
//~^ useless_asref

trait Identity {
fn id(self) -> Self
where
Self: Sized,
{
self
}
}
impl Identity for &str {}

let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(s.id().as_ref())).collect();
//~^ useless_asref

let v: Vec<Cow<'_, str>> = exts
.iter()
.map(|s| Cow::Borrowed(std::convert::identity(s).as_ref()))
//~^ useless_asref
.collect();

struct Wrapper<'a>(&'a str);
let exts_field: Vec<Wrapper> = exts.iter().map(|s| Wrapper(s)).collect();
let v: Vec<Cow<'_, str>> = exts_field.iter().map(|w| Cow::Borrowed(w.0.as_ref())).collect();
//~^ useless_asref

let exts_index: Vec<&[&str]> = exts.iter().map(|s| std::slice::from_ref(s)).collect();
let v: Vec<Cow<'_, str>> = exts_index.iter().map(|arr| Cow::Borrowed(arr[0].as_ref())).collect();
//~^ useless_asref
}

fn main() {
not_ok();
ok();
Expand Down
32 changes: 31 additions & 1 deletion tests/ui/useless_asref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,35 @@ error: this call to `as_ref.map(...)` does nothing
LL | Some(1).as_ref().map(|&x| x.clone());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(1).clone()`

error: aborting due to 18 previous errors
error: this call to `as_ref` does nothing
--> tests/ui/useless_asref.rs:264:66
|
LL | let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(s.as_ref())).collect();
| ^^^^^^^^^^ help: try: `*s`

error: this call to `as_ref` does nothing
--> tests/ui/useless_asref.rs:277:66
|
LL | let v: Vec<Cow<'_, str>> = exts.iter().map(|s| Cow::Borrowed(s.id().as_ref())).collect();
| ^^^^^^^^^^^^^^^ help: try: `s.id()`

error: this call to `as_ref` does nothing
--> tests/ui/useless_asref.rs:282:32
|
LL | .map(|s| Cow::Borrowed(std::convert::identity(s).as_ref()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*std::convert::identity(s)`

error: this call to `as_ref` does nothing
--> tests/ui/useless_asref.rs:288:72
|
LL | let v: Vec<Cow<'_, str>> = exts_field.iter().map(|w| Cow::Borrowed(w.0.as_ref())).collect();
| ^^^^^^^^^^^^ help: try: `w.0`

error: this call to `as_ref` does nothing
--> tests/ui/useless_asref.rs:292:74
|
LL | let v: Vec<Cow<'_, str>> = exts_index.iter().map(|arr| Cow::Borrowed(arr[0].as_ref())).collect();
| ^^^^^^^^^^^^^^^ help: try: `arr[0]`

error: aborting due to 23 previous errors

Loading