Add explicit_auto_deref
lint
This commit is contained in:
parent
c107c97e69
commit
8a74d33570
@ -3400,6 +3400,7 @@ Released 2018-09-13
|
||||
[`expect_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
|
||||
[`expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_used
|
||||
[`expl_impl_clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy
|
||||
[`explicit_auto_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref
|
||||
[`explicit_counter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop
|
||||
[`explicit_deref_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods
|
||||
[`explicit_into_iter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_into_iter_loop
|
||||
|
@ -1,18 +1,21 @@
|
||||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
|
||||
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
|
||||
use clippy_utils::sugg::has_enclosing_paren;
|
||||
use clippy_utils::ty::peel_mid_ty_refs;
|
||||
use clippy_utils::{get_parent_expr, get_parent_node, is_lint_allowed, path_to_local};
|
||||
use clippy_utils::ty::{expr_sig, peel_mid_ty_refs, variant_of_res};
|
||||
use clippy_utils::{
|
||||
get_parent_expr, get_parent_node, is_lint_allowed, path_to_local, peel_hir_ty_refs, walk_to_expr_usage,
|
||||
};
|
||||
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
|
||||
use rustc_data_structures::fx::FxIndexMap;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{
|
||||
BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, HirId, MatchSource, Mutability, Node,
|
||||
Pat, PatKind, UnOp,
|
||||
self as hir, BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, FnRetTy, GenericArg, HirId,
|
||||
ImplItem, ImplItemKind, Item, ItemKind, Local, MatchSource, Mutability, Node, Pat, PatKind, Path, QPath, TraitItem,
|
||||
TraitItemKind, TyKind, UnOp,
|
||||
};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeckResults};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeckResults};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::{symbol::sym, Span};
|
||||
|
||||
@ -104,10 +107,34 @@ declare_clippy_lint! {
|
||||
"`ref` binding to a reference"
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
/// Checks for dereferencing expressions which would be covered by auto-deref.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// This unnecessarily complicates the code.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```rust
|
||||
/// let x = String::new();
|
||||
/// let y: &str = &*x;
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// let x = String::new();
|
||||
/// let y: &str = &x;
|
||||
/// ```
|
||||
#[clippy::version = "1.60.0"]
|
||||
pub EXPLICIT_AUTO_DEREF,
|
||||
complexity,
|
||||
"dereferencing when the compiler would automatically dereference"
|
||||
}
|
||||
|
||||
impl_lint_pass!(Dereferencing => [
|
||||
EXPLICIT_DEREF_METHODS,
|
||||
NEEDLESS_BORROW,
|
||||
REF_BINDING_TO_REFERENCE,
|
||||
EXPLICIT_AUTO_DEREF,
|
||||
]);
|
||||
|
||||
#[derive(Default)]
|
||||
@ -152,6 +179,11 @@ enum State {
|
||||
required_precedence: i8,
|
||||
msg: &'static str,
|
||||
},
|
||||
ExplicitDeref {
|
||||
deref_span: Span,
|
||||
deref_hir_id: HirId,
|
||||
},
|
||||
Borrow,
|
||||
}
|
||||
|
||||
// A reference operation considered by this lint pass
|
||||
@ -305,6 +337,14 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
|
||||
hir_id: expr.hir_id,
|
||||
},
|
||||
));
|
||||
} else if is_stable_auto_deref_position(cx, expr) {
|
||||
self.state = Some((
|
||||
State::Borrow,
|
||||
StateData {
|
||||
span: expr.span,
|
||||
hir_id: expr.hir_id,
|
||||
},
|
||||
));
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
@ -354,6 +394,18 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
|
||||
data,
|
||||
));
|
||||
},
|
||||
(Some((State::Borrow, data)), RefOp::Deref) => {
|
||||
self.state = Some((
|
||||
State::ExplicitDeref {
|
||||
deref_span: expr.span,
|
||||
deref_hir_id: expr.hir_id,
|
||||
},
|
||||
data,
|
||||
));
|
||||
},
|
||||
(state @ Some((State::ExplicitDeref { .. }, _)), RefOp::Deref) => {
|
||||
self.state = state;
|
||||
},
|
||||
|
||||
(Some((state, data)), _) => report(cx, expr, state, data),
|
||||
}
|
||||
@ -596,8 +648,230 @@ fn find_adjustments<'tcx>(
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if the expression for the given id occurs in a position which auto dereferencing applies.
|
||||
// Note that the target type must not be inferred in a way that may cause auto-deref to select a
|
||||
// different type, nor may the position be the result of a macro expansion.
|
||||
//
|
||||
// e.g. the following should not linted
|
||||
// macro_rules! foo { ($e:expr) => { let x: &str = $e; }}
|
||||
// foo!(&*String::new());
|
||||
// fn foo<T>(_: &T) {}
|
||||
// foo(&*String::new())
|
||||
fn is_stable_auto_deref_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
|
||||
walk_to_expr_usage(cx, e, |node, child_id| match node {
|
||||
Node::Local(&Local { ty: Some(ty), .. }) => Some(is_binding_ty_auto_deref_stable(ty)),
|
||||
Node::Item(&Item {
|
||||
kind: ItemKind::Static(..) | ItemKind::Const(..),
|
||||
..
|
||||
})
|
||||
| Node::TraitItem(&TraitItem {
|
||||
kind: TraitItemKind::Const(..),
|
||||
..
|
||||
})
|
||||
| Node::ImplItem(&ImplItem {
|
||||
kind: ImplItemKind::Const(..),
|
||||
..
|
||||
}) => Some(true),
|
||||
|
||||
Node::Item(&Item {
|
||||
kind: ItemKind::Fn(..),
|
||||
def_id,
|
||||
..
|
||||
})
|
||||
| Node::TraitItem(&TraitItem {
|
||||
kind: TraitItemKind::Fn(..),
|
||||
def_id,
|
||||
..
|
||||
})
|
||||
| Node::ImplItem(&ImplItem {
|
||||
kind: ImplItemKind::Fn(..),
|
||||
def_id,
|
||||
..
|
||||
}) => {
|
||||
let output = cx.tcx.fn_sig(def_id.to_def_id()).skip_binder().output();
|
||||
Some(!(output.has_placeholders() || output.has_opaque_types()))
|
||||
},
|
||||
|
||||
Node::Expr(e) => match e.kind {
|
||||
ExprKind::Ret(_) => {
|
||||
let output = cx
|
||||
.tcx
|
||||
.fn_sig(cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap()))
|
||||
.skip_binder()
|
||||
.output();
|
||||
Some(!(output.has_placeholders() || output.has_opaque_types()))
|
||||
},
|
||||
ExprKind::Call(func, args) => Some(
|
||||
args.iter()
|
||||
.position(|arg| arg.hir_id == child_id)
|
||||
.zip(expr_sig(cx, func))
|
||||
.and_then(|(i, sig)| sig.input_with_hir(i))
|
||||
.map_or(false, |(hir_ty, ty)| match hir_ty {
|
||||
// Type inference for closures can depend on how they're called. Only go by the explicit
|
||||
// types here.
|
||||
Some(ty) => is_binding_ty_auto_deref_stable(ty),
|
||||
None => is_param_auto_deref_stable(ty.skip_binder()),
|
||||
}),
|
||||
),
|
||||
ExprKind::MethodCall(_, [_, args @ ..], _) => {
|
||||
let id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
|
||||
Some(args.iter().position(|arg| arg.hir_id == child_id).map_or(false, |i| {
|
||||
let arg = cx.tcx.fn_sig(id).skip_binder().inputs()[i + 1];
|
||||
is_param_auto_deref_stable(arg)
|
||||
}))
|
||||
},
|
||||
ExprKind::Struct(path, fields, _) => {
|
||||
let variant = variant_of_res(cx, cx.qpath_res(path, e.hir_id));
|
||||
Some(
|
||||
fields
|
||||
.iter()
|
||||
.find(|f| f.expr.hir_id == child_id)
|
||||
.zip(variant)
|
||||
.and_then(|(field, variant)| variant.fields.iter().find(|f| f.name == field.ident.name))
|
||||
.map_or(false, |field| is_param_auto_deref_stable(cx.tcx.type_of(field.did))),
|
||||
)
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
// Checks whether auto-dereferencing any type into a binding of the given type will definitely
|
||||
// produce the same result.
|
||||
//
|
||||
// e.g.
|
||||
// let x = Box::new(Box::new(0u32));
|
||||
// let y1: &Box<_> = x.deref();
|
||||
// let y2: &Box<_> = &x;
|
||||
//
|
||||
// Here `y1` and `y2` would resolve to different types, so the type `&Box<_>` is not stable when
|
||||
// switching to auto-dereferencing.
|
||||
fn is_binding_ty_auto_deref_stable(ty: &hir::Ty<'_>) -> bool {
|
||||
let (ty, count) = peel_hir_ty_refs(ty);
|
||||
if count != 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
match &ty.kind {
|
||||
TyKind::Rptr(_, ty) => is_binding_ty_auto_deref_stable(ty.ty),
|
||||
&TyKind::Path(
|
||||
QPath::TypeRelative(_, path)
|
||||
| QPath::Resolved(
|
||||
_,
|
||||
Path {
|
||||
segments: [.., path], ..
|
||||
},
|
||||
),
|
||||
) => {
|
||||
if let Some(args) = path.args {
|
||||
args.args.iter().all(|arg| {
|
||||
if let GenericArg::Type(ty) = arg {
|
||||
!ty_contains_infer(ty)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
} else {
|
||||
true
|
||||
}
|
||||
},
|
||||
TyKind::Slice(_)
|
||||
| TyKind::Array(..)
|
||||
| TyKind::BareFn(_)
|
||||
| TyKind::Never
|
||||
| TyKind::Tup(_)
|
||||
| TyKind::Ptr(_)
|
||||
| TyKind::TraitObject(..)
|
||||
| TyKind::Path(_) => true,
|
||||
TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(..) | TyKind::Err => false,
|
||||
}
|
||||
}
|
||||
|
||||
// Checks whether a type is inferred at some point.
|
||||
// e.g. `_`, `Box<_>`, `[_]`
|
||||
fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool {
|
||||
match &ty.kind {
|
||||
TyKind::Slice(ty) | TyKind::Array(ty, _) => ty_contains_infer(ty),
|
||||
TyKind::Ptr(ty) | TyKind::Rptr(_, ty) => ty_contains_infer(ty.ty),
|
||||
TyKind::Tup(tys) => tys.iter().any(ty_contains_infer),
|
||||
TyKind::BareFn(ty) => {
|
||||
if ty.decl.inputs.iter().any(ty_contains_infer) {
|
||||
return true;
|
||||
}
|
||||
if let FnRetTy::Return(ty) = &ty.decl.output {
|
||||
ty_contains_infer(ty)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
&TyKind::Path(
|
||||
QPath::TypeRelative(_, path)
|
||||
| QPath::Resolved(
|
||||
_,
|
||||
Path {
|
||||
segments: [.., path], ..
|
||||
},
|
||||
),
|
||||
) => {
|
||||
if let Some(args) = path.args {
|
||||
args.args.iter().any(|arg| {
|
||||
if let GenericArg::Type(ty) = arg {
|
||||
ty_contains_infer(ty)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
TyKind::Path(_) | TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(_) | TyKind::Err => true,
|
||||
TyKind::Never | TyKind::TraitObject(..) => false,
|
||||
}
|
||||
}
|
||||
|
||||
// Checks whether a type is stable when switching to auto dereferencing,
|
||||
fn is_param_auto_deref_stable(ty: Ty<'_>) -> bool {
|
||||
let (ty, count) = peel_mid_ty_refs(ty);
|
||||
if count != 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
match ty.kind() {
|
||||
ty::Bool
|
||||
| ty::Char
|
||||
| ty::Int(_)
|
||||
| ty::Uint(_)
|
||||
| ty::Float(_)
|
||||
| ty::Foreign(_)
|
||||
| ty::Str
|
||||
| ty::Array(..)
|
||||
| ty::Slice(..)
|
||||
| ty::RawPtr(..)
|
||||
| ty::FnDef(..)
|
||||
| ty::FnPtr(_)
|
||||
| ty::Closure(..)
|
||||
| ty::Generator(..)
|
||||
| ty::GeneratorWitness(..)
|
||||
| ty::Never
|
||||
| ty::Tuple(_)
|
||||
| ty::Ref(..)
|
||||
| ty::Projection(_) => true,
|
||||
ty::Infer(_)
|
||||
| ty::Error(_)
|
||||
| ty::Param(_)
|
||||
| ty::Bound(..)
|
||||
| ty::Opaque(..)
|
||||
| ty::Placeholder(_)
|
||||
| ty::Dynamic(..) => false,
|
||||
ty::Adt(..) => !(ty.has_placeholders() || ty.has_param_types_or_consts()),
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(clippy::needless_pass_by_value)]
|
||||
fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: StateData) {
|
||||
fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data: StateData) {
|
||||
match state {
|
||||
State::DerefMethod {
|
||||
ty_changed_count,
|
||||
@ -663,6 +937,29 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
|
||||
diag.span_suggestion(data.span, "change this to", sugg, app);
|
||||
});
|
||||
},
|
||||
State::ExplicitDeref {
|
||||
deref_span,
|
||||
deref_hir_id,
|
||||
} => {
|
||||
let (span, hir_id) = if cx.typeck_results().expr_ty(expr).is_ref() {
|
||||
(data.span, data.hir_id)
|
||||
} else {
|
||||
(deref_span, deref_hir_id)
|
||||
};
|
||||
span_lint_hir_and_then(
|
||||
cx,
|
||||
EXPLICIT_AUTO_DEREF,
|
||||
hir_id,
|
||||
span,
|
||||
"deref which would be done by auto-deref",
|
||||
|diag| {
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
let snip = snippet_with_context(cx, expr.span, span.ctxt(), "..", &mut app).0;
|
||||
diag.span_suggestion(span, "try this", snip.into_owned(), app);
|
||||
},
|
||||
);
|
||||
},
|
||||
State::Borrow => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
|
||||
LintId::of(crate_in_macro_def::CRATE_IN_MACRO_DEF),
|
||||
LintId::of(default::FIELD_REASSIGN_WITH_DEFAULT),
|
||||
LintId::of(default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY),
|
||||
LintId::of(dereference::EXPLICIT_AUTO_DEREF),
|
||||
LintId::of(dereference::NEEDLESS_BORROW),
|
||||
LintId::of(derivable_impls::DERIVABLE_IMPLS),
|
||||
LintId::of(derive::DERIVE_HASH_XOR_EQ),
|
||||
|
@ -9,6 +9,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
|
||||
LintId::of(bytes_count_to_len::BYTES_COUNT_TO_LEN),
|
||||
LintId::of(casts::CHAR_LIT_AS_U8),
|
||||
LintId::of(casts::UNNECESSARY_CAST),
|
||||
LintId::of(dereference::EXPLICIT_AUTO_DEREF),
|
||||
LintId::of(derivable_impls::DERIVABLE_IMPLS),
|
||||
LintId::of(double_comparison::DOUBLE_COMPARISONS),
|
||||
LintId::of(double_parens::DOUBLE_PARENS),
|
||||
|
@ -110,6 +110,7 @@ store.register_lints(&[
|
||||
default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY,
|
||||
default_numeric_fallback::DEFAULT_NUMERIC_FALLBACK,
|
||||
default_union_representation::DEFAULT_UNION_REPRESENTATION,
|
||||
dereference::EXPLICIT_AUTO_DEREF,
|
||||
dereference::EXPLICIT_DEREF_METHODS,
|
||||
dereference::NEEDLESS_BORROW,
|
||||
dereference::REF_BINDING_TO_REFERENCE,
|
||||
|
@ -118,7 +118,7 @@ fn lint(cx: &LateContext<'_>, case_method: &CaseMethod, bad_case_span: Span, bad
|
||||
MATCH_STR_CASE_MISMATCH,
|
||||
bad_case_span,
|
||||
"this `match` arm has a differing case than its expression",
|
||||
&*format!("consider changing the case of this arm to respect `{}`", method_str),
|
||||
&format!("consider changing the case of this arm to respect `{}`", method_str),
|
||||
format!("\"{}\"", suggestion),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
|
@ -326,7 +326,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
|
||||
// add the pattern after the expression because the bindings aren't available
|
||||
// yet in the init
|
||||
// expression
|
||||
SimilarNamesNameVisitor(self).visit_pat(&*local.pat);
|
||||
SimilarNamesNameVisitor(self).visit_pat(&local.pat);
|
||||
}
|
||||
fn visit_block(&mut self, blk: &'tcx Block) {
|
||||
self.single_char_names.push(vec![]);
|
||||
|
@ -574,14 +574,13 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
|
||||
Some((Node::Expr(e), child_id)) => match e.kind {
|
||||
ExprKind::Call(f, expr_args) => {
|
||||
let i = expr_args.iter().position(|arg| arg.hir_id == child_id).unwrap_or(0);
|
||||
if expr_sig(self.cx, f)
|
||||
.map(|sig| sig.input(i).skip_binder().peel_refs())
|
||||
.map_or(true, |ty| match *ty.kind() {
|
||||
if expr_sig(self.cx, f).and_then(|sig| sig.input(i)).map_or(true, |ty| {
|
||||
match *ty.skip_binder().peel_refs().kind() {
|
||||
ty::Param(_) => true,
|
||||
ty::Adt(def, _) => def.did() == args.ty_did,
|
||||
_ => false,
|
||||
})
|
||||
{
|
||||
}
|
||||
}) {
|
||||
// Passed to a function taking the non-dereferenced type.
|
||||
set_skip_flag();
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ impl RedundantStaticLifetimes {
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
self.visit_type(&*borrow_type.ty, cx, reason);
|
||||
self.visit_type(&borrow_type.ty, cx, reason);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
@ -2058,6 +2058,21 @@ pub fn peel_hir_expr_refs<'a>(expr: &'a Expr<'a>) -> (&'a Expr<'a>, usize) {
|
||||
(e, count)
|
||||
}
|
||||
|
||||
/// Peels off all references on the type. Returns the underlying type and the number of references
|
||||
/// removed.
|
||||
pub fn peel_hir_ty_refs<'a>(mut ty: &'a hir::Ty<'a>) -> (&'a hir::Ty<'a>, usize) {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
match &ty.kind {
|
||||
TyKind::Rptr(_, ref_ty) => {
|
||||
ty = ref_ty.ty;
|
||||
count += 1;
|
||||
},
|
||||
_ => break (ty, count),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes `AddrOf` operators (`&`) or deref operators (`*`), but only if a reference type is
|
||||
/// dereferenced. An overloaded deref such as `Vec` to slice would not be removed.
|
||||
pub fn peel_ref_operators<'hir>(cx: &LateContext<'_>, mut expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
|
||||
@ -2110,7 +2125,7 @@ fn with_test_item_names<'tcx>(tcx: TyCtxt<'tcx>, module: LocalDefId, f: impl Fn(
|
||||
}
|
||||
}
|
||||
names.sort_unstable();
|
||||
f(&*entry.insert(names))
|
||||
f(entry.insert(names))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -2168,6 +2183,57 @@ pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
|
||||
&& item.ident.name.as_str().split('_').any(|a| a == "test" || a == "tests")
|
||||
}
|
||||
|
||||
/// Walks the HIR tree from the given expression, up to the node where the value produced by the
|
||||
/// expression is consumed. Calls the function for every node encountered this way until it returns
|
||||
/// `Some`.
|
||||
///
|
||||
/// This allows walking through `if`, `match`, `break`, block expressions to find where the value
|
||||
/// produced by the expression is consumed.
|
||||
pub fn walk_to_expr_usage<'tcx, T>(
|
||||
cx: &LateContext<'tcx>,
|
||||
e: &Expr<'tcx>,
|
||||
mut f: impl FnMut(Node<'tcx>, HirId) -> Option<T>,
|
||||
) -> Option<T> {
|
||||
let map = cx.tcx.hir();
|
||||
let mut iter = map.parent_iter(e.hir_id);
|
||||
let mut child_id = e.hir_id;
|
||||
|
||||
while let Some((parent_id, parent)) = iter.next() {
|
||||
if let Some(x) = f(parent, child_id) {
|
||||
return Some(x);
|
||||
}
|
||||
let parent = match parent {
|
||||
Node::Expr(e) => e,
|
||||
Node::Block(Block { expr: Some(body), .. }) | Node::Arm(Arm { body, .. }) if body.hir_id == child_id => {
|
||||
child_id = parent_id;
|
||||
continue;
|
||||
},
|
||||
Node::Arm(a) if a.body.hir_id == child_id => {
|
||||
child_id = parent_id;
|
||||
continue;
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
match parent.kind {
|
||||
ExprKind::If(child, ..) | ExprKind::Match(child, ..) if child.hir_id != child_id => {
|
||||
child_id = parent_id;
|
||||
continue;
|
||||
},
|
||||
ExprKind::Break(Destination { target_id: Ok(id), .. }, _) => {
|
||||
child_id = id;
|
||||
iter = map.parent_iter(id);
|
||||
continue;
|
||||
},
|
||||
ExprKind::Block(..) => {
|
||||
child_id = parent_id;
|
||||
continue;
|
||||
},
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
macro_rules! op_utils {
|
||||
($($name:ident $assign:ident)*) => {
|
||||
/// Binary operation traits like `LangItem::Add`
|
||||
|
@ -6,16 +6,16 @@ use core::ops::ControlFlow;
|
||||
use rustc_ast::ast::Mutability;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{CtorKind, DefKind, Res};
|
||||
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::{Expr, LangItem, TyKind, Unsafety};
|
||||
use rustc_hir::{Expr, FnDecl, LangItem, TyKind, Unsafety};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
||||
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
|
||||
use rustc_middle::ty::{
|
||||
self, AdtDef, Binder, BoundRegion, FnSig, IntTy, ParamEnv, Predicate, PredicateKind, ProjectionTy, Region,
|
||||
RegionKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitor, UintTy, VariantDiscr,
|
||||
self, AdtDef, Binder, BoundRegion, DefIdTree, FnSig, IntTy, ParamEnv, Predicate, PredicateKind, ProjectionTy,
|
||||
Region, RegionKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitor, UintTy, VariantDef, VariantDiscr,
|
||||
};
|
||||
use rustc_span::symbol::Ident;
|
||||
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
|
||||
@ -502,16 +502,46 @@ pub fn all_predicates_of(tcx: TyCtxt<'_>, id: DefId) -> impl Iterator<Item = &(P
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum ExprFnSig<'tcx> {
|
||||
Sig(Binder<'tcx, FnSig<'tcx>>),
|
||||
Closure(Binder<'tcx, FnSig<'tcx>>),
|
||||
Closure(Option<&'tcx FnDecl<'tcx>>, Binder<'tcx, FnSig<'tcx>>),
|
||||
Trait(Binder<'tcx, Ty<'tcx>>, Option<Binder<'tcx, Ty<'tcx>>>),
|
||||
}
|
||||
impl<'tcx> ExprFnSig<'tcx> {
|
||||
/// Gets the argument type at the given offset.
|
||||
pub fn input(self, i: usize) -> Binder<'tcx, Ty<'tcx>> {
|
||||
/// Gets the argument type at the given offset. This will return `None` when the index is out of
|
||||
/// bounds only for variadic functions, otherwise this will panic.
|
||||
pub fn input(self, i: usize) -> Option<Binder<'tcx, Ty<'tcx>>> {
|
||||
match self {
|
||||
Self::Sig(sig) => sig.input(i),
|
||||
Self::Closure(sig) => sig.input(0).map_bound(|ty| ty.tuple_fields()[i]),
|
||||
Self::Trait(inputs, _) => inputs.map_bound(|ty| ty.tuple_fields()[i]),
|
||||
Self::Sig(sig) => {
|
||||
if sig.c_variadic() {
|
||||
sig.inputs().map_bound(|inputs| inputs.get(i).copied()).transpose()
|
||||
} else {
|
||||
Some(sig.input(i))
|
||||
}
|
||||
},
|
||||
Self::Closure(_, sig) => Some(sig.input(0).map_bound(|ty| ty.tuple_fields()[i])),
|
||||
Self::Trait(inputs, _) => Some(inputs.map_bound(|ty| ty.tuple_fields()[i])),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the argument type at the given offset. For closures this will also get the type as
|
||||
/// written. This will return `None` when the index is out of bounds only for variadic
|
||||
/// functions, otherwise this will panic.
|
||||
pub fn input_with_hir(self, i: usize) -> Option<(Option<&'tcx hir::Ty<'tcx>>, Binder<'tcx, Ty<'tcx>>)> {
|
||||
match self {
|
||||
Self::Sig(sig) => {
|
||||
if sig.c_variadic() {
|
||||
sig.inputs()
|
||||
.map_bound(|inputs| inputs.get(i).copied())
|
||||
.transpose()
|
||||
.map(|arg| (None, arg))
|
||||
} else {
|
||||
Some((None, sig.input(i)))
|
||||
}
|
||||
},
|
||||
Self::Closure(decl, sig) => Some((
|
||||
decl.and_then(|decl| decl.inputs.get(i)),
|
||||
sig.input(0).map_bound(|ty| ty.tuple_fields()[i]),
|
||||
)),
|
||||
Self::Trait(inputs, _) => Some((None, inputs.map_bound(|ty| ty.tuple_fields()[i]))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -519,7 +549,7 @@ impl<'tcx> ExprFnSig<'tcx> {
|
||||
/// specified.
|
||||
pub fn output(self) -> Option<Binder<'tcx, Ty<'tcx>>> {
|
||||
match self {
|
||||
Self::Sig(sig) | Self::Closure(sig) => Some(sig.output()),
|
||||
Self::Sig(sig) | Self::Closure(_, sig) => Some(sig.output()),
|
||||
Self::Trait(_, output) => output,
|
||||
}
|
||||
}
|
||||
@ -536,7 +566,12 @@ pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnS
|
||||
|
||||
fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'tcx>> {
|
||||
match *ty.kind() {
|
||||
ty::Closure(_, subs) => Some(ExprFnSig::Closure(subs.as_closure().sig())),
|
||||
ty::Closure(id, subs) => {
|
||||
let decl = id
|
||||
.as_local()
|
||||
.and_then(|id| cx.tcx.hir().fn_decl_by_hir_id(cx.tcx.hir().local_def_id_to_hir_id(id)));
|
||||
Some(ExprFnSig::Closure(decl, subs.as_closure().sig()))
|
||||
},
|
||||
ty::FnDef(id, subs) => Some(ExprFnSig::Sig(cx.tcx.bound_fn_sig(id).subst(cx.tcx, subs))),
|
||||
ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig)),
|
||||
ty::Dynamic(bounds, _) => {
|
||||
@ -739,3 +774,17 @@ pub fn for_each_top_level_late_bound_region<B>(
|
||||
}
|
||||
ty.visit_with(&mut V { index: 0, f })
|
||||
}
|
||||
|
||||
pub fn variant_of_res<'tcx>(cx: &LateContext<'tcx>, res: Res) -> Option<&'tcx VariantDef> {
|
||||
match res {
|
||||
Res::Def(DefKind::Struct, id) => Some(cx.tcx.adt_def(id).non_enum_variant()),
|
||||
Res::Def(DefKind::Variant, id) => Some(cx.tcx.adt_def(cx.tcx.parent(id)).variant_with_id(id)),
|
||||
Res::Def(DefKind::Ctor(CtorOf::Struct, _), id) => Some(cx.tcx.adt_def(cx.tcx.parent(id)).non_enum_variant()),
|
||||
Res::Def(DefKind::Ctor(CtorOf::Variant, _), id) => {
|
||||
let var_id = cx.tcx.parent(id);
|
||||
Some(cx.tcx.adt_def(cx.tcx.parent(var_id)).variant_with_id(var_id))
|
||||
},
|
||||
Res::SelfCtor(id) => Some(cx.tcx.type_of(id).ty_adt_def().unwrap().non_enum_variant()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![allow(dead_code, unused_variables)]
|
||||
#![allow(dead_code, unused_variables, clippy::explicit_auto_deref)]
|
||||
|
||||
fn main() {}
|
||||
|
||||
|
161
tests/ui/explicit_auto_deref.fixed
Normal file
161
tests/ui/explicit_auto_deref.fixed
Normal file
@ -0,0 +1,161 @@
|
||||
// run-rustfix
|
||||
|
||||
#![warn(clippy::explicit_auto_deref)]
|
||||
#![allow(
|
||||
dead_code,
|
||||
unused_braces,
|
||||
clippy::borrowed_box,
|
||||
clippy::needless_borrow,
|
||||
clippy::ptr_arg,
|
||||
clippy::redundant_field_names,
|
||||
clippy::too_many_arguments
|
||||
)]
|
||||
|
||||
trait CallableStr {
|
||||
type T: Fn(&str);
|
||||
fn callable_str(&self) -> Self::T;
|
||||
}
|
||||
impl CallableStr for () {
|
||||
type T = fn(&str);
|
||||
fn callable_str(&self) -> Self::T {
|
||||
fn f(_: &str) {}
|
||||
f
|
||||
}
|
||||
}
|
||||
impl CallableStr for i32 {
|
||||
type T = <() as CallableStr>::T;
|
||||
fn callable_str(&self) -> Self::T {
|
||||
().callable_str()
|
||||
}
|
||||
}
|
||||
|
||||
trait CallableT<U: ?Sized> {
|
||||
type T: Fn(&U);
|
||||
fn callable_t(&self) -> Self::T;
|
||||
}
|
||||
impl<U: ?Sized> CallableT<U> for () {
|
||||
type T = fn(&U);
|
||||
fn callable_t(&self) -> Self::T {
|
||||
fn f<U: ?Sized>(_: &U) {}
|
||||
f::<U>
|
||||
}
|
||||
}
|
||||
impl<U: ?Sized> CallableT<U> for i32 {
|
||||
type T = <() as CallableT<U>>::T;
|
||||
fn callable_t(&self) -> Self::T {
|
||||
().callable_t()
|
||||
}
|
||||
}
|
||||
|
||||
fn f_str(_: &str) {}
|
||||
fn f_t<T>(_: T) {}
|
||||
fn f_ref_t<T: ?Sized>(_: &T) {}
|
||||
|
||||
fn f_str_t<T>(_: &str, _: T) {}
|
||||
|
||||
fn f_box_t<T>(_: &Box<T>) {}
|
||||
|
||||
fn main() {
|
||||
let s = String::new();
|
||||
|
||||
let _: &str = &s;
|
||||
let _ = &*s; // Don't lint. Inferred type would change.
|
||||
let _: &_ = &*s; // Don't lint. Inferred type would change.
|
||||
|
||||
f_str(&s);
|
||||
f_t(&*s); // Don't lint. Inferred type would change.
|
||||
f_ref_t(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
f_str_t(&s, &*s); // Don't lint second param.
|
||||
|
||||
let b = Box::new(Box::new(Box::new(5)));
|
||||
let _: &Box<i32> = &b;
|
||||
let _: &Box<_> = &**b; // Don't lint. Inferred type would change.
|
||||
|
||||
f_box_t(&**b); // Don't lint. Inferred type would change.
|
||||
|
||||
let c = |_x: &str| ();
|
||||
c(&s);
|
||||
|
||||
let c = |_x| ();
|
||||
c(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
fn _f(x: &String) -> &str {
|
||||
x
|
||||
}
|
||||
|
||||
fn _f1(x: &String) -> &str {
|
||||
{ x }
|
||||
}
|
||||
|
||||
fn _f2(x: &String) -> &str {
|
||||
{ x }
|
||||
}
|
||||
|
||||
fn _f3(x: &Box<Box<Box<i32>>>) -> &Box<i32> {
|
||||
x
|
||||
}
|
||||
|
||||
fn _f4(
|
||||
x: String,
|
||||
f1: impl Fn(&str),
|
||||
f2: &dyn Fn(&str),
|
||||
f3: fn(&str),
|
||||
f4: impl CallableStr,
|
||||
f5: <() as CallableStr>::T,
|
||||
f6: <i32 as CallableStr>::T,
|
||||
f7: &dyn CallableStr<T = fn(&str)>,
|
||||
f8: impl CallableT<str>,
|
||||
f9: <() as CallableT<str>>::T,
|
||||
f10: <i32 as CallableT<str>>::T,
|
||||
f11: &dyn CallableT<str, T = fn(&str)>,
|
||||
) {
|
||||
f1(&x);
|
||||
f2(&x);
|
||||
f3(&x);
|
||||
f4.callable_str()(&x);
|
||||
f5(&x);
|
||||
f6(&x);
|
||||
f7.callable_str()(&x);
|
||||
f8.callable_t()(&x);
|
||||
f9(&x);
|
||||
f10(&x);
|
||||
f11.callable_t()(&x);
|
||||
}
|
||||
|
||||
struct S1<'a>(&'a str);
|
||||
let _ = S1(&s);
|
||||
|
||||
struct S2<'a> {
|
||||
s: &'a str,
|
||||
}
|
||||
let _ = S2 { s: &s };
|
||||
|
||||
struct S3<'a, T: ?Sized>(&'a T);
|
||||
let _ = S3(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
struct S4<'a, T: ?Sized> {
|
||||
s: &'a T,
|
||||
}
|
||||
let _ = S4 { s: &*s }; // Don't lint. Inferred type would change.
|
||||
|
||||
enum E1<'a> {
|
||||
S1(&'a str),
|
||||
S2 { s: &'a str },
|
||||
}
|
||||
impl<'a> E1<'a> {
|
||||
fn m1(s: &'a String) {
|
||||
let _ = Self::S1(s);
|
||||
let _ = Self::S2 { s: s };
|
||||
}
|
||||
}
|
||||
let _ = E1::S1(&s);
|
||||
let _ = E1::S2 { s: &s };
|
||||
|
||||
enum E2<'a, T: ?Sized> {
|
||||
S1(&'a T),
|
||||
S2 { s: &'a T },
|
||||
}
|
||||
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
|
||||
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
|
||||
}
|
161
tests/ui/explicit_auto_deref.rs
Normal file
161
tests/ui/explicit_auto_deref.rs
Normal file
@ -0,0 +1,161 @@
|
||||
// run-rustfix
|
||||
|
||||
#![warn(clippy::explicit_auto_deref)]
|
||||
#![allow(
|
||||
dead_code,
|
||||
unused_braces,
|
||||
clippy::borrowed_box,
|
||||
clippy::needless_borrow,
|
||||
clippy::ptr_arg,
|
||||
clippy::redundant_field_names,
|
||||
clippy::too_many_arguments
|
||||
)]
|
||||
|
||||
trait CallableStr {
|
||||
type T: Fn(&str);
|
||||
fn callable_str(&self) -> Self::T;
|
||||
}
|
||||
impl CallableStr for () {
|
||||
type T = fn(&str);
|
||||
fn callable_str(&self) -> Self::T {
|
||||
fn f(_: &str) {}
|
||||
f
|
||||
}
|
||||
}
|
||||
impl CallableStr for i32 {
|
||||
type T = <() as CallableStr>::T;
|
||||
fn callable_str(&self) -> Self::T {
|
||||
().callable_str()
|
||||
}
|
||||
}
|
||||
|
||||
trait CallableT<U: ?Sized> {
|
||||
type T: Fn(&U);
|
||||
fn callable_t(&self) -> Self::T;
|
||||
}
|
||||
impl<U: ?Sized> CallableT<U> for () {
|
||||
type T = fn(&U);
|
||||
fn callable_t(&self) -> Self::T {
|
||||
fn f<U: ?Sized>(_: &U) {}
|
||||
f::<U>
|
||||
}
|
||||
}
|
||||
impl<U: ?Sized> CallableT<U> for i32 {
|
||||
type T = <() as CallableT<U>>::T;
|
||||
fn callable_t(&self) -> Self::T {
|
||||
().callable_t()
|
||||
}
|
||||
}
|
||||
|
||||
fn f_str(_: &str) {}
|
||||
fn f_t<T>(_: T) {}
|
||||
fn f_ref_t<T: ?Sized>(_: &T) {}
|
||||
|
||||
fn f_str_t<T>(_: &str, _: T) {}
|
||||
|
||||
fn f_box_t<T>(_: &Box<T>) {}
|
||||
|
||||
fn main() {
|
||||
let s = String::new();
|
||||
|
||||
let _: &str = &*s;
|
||||
let _ = &*s; // Don't lint. Inferred type would change.
|
||||
let _: &_ = &*s; // Don't lint. Inferred type would change.
|
||||
|
||||
f_str(&*s);
|
||||
f_t(&*s); // Don't lint. Inferred type would change.
|
||||
f_ref_t(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
f_str_t(&*s, &*s); // Don't lint second param.
|
||||
|
||||
let b = Box::new(Box::new(Box::new(5)));
|
||||
let _: &Box<i32> = &**b;
|
||||
let _: &Box<_> = &**b; // Don't lint. Inferred type would change.
|
||||
|
||||
f_box_t(&**b); // Don't lint. Inferred type would change.
|
||||
|
||||
let c = |_x: &str| ();
|
||||
c(&*s);
|
||||
|
||||
let c = |_x| ();
|
||||
c(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
fn _f(x: &String) -> &str {
|
||||
&**x
|
||||
}
|
||||
|
||||
fn _f1(x: &String) -> &str {
|
||||
{ &**x }
|
||||
}
|
||||
|
||||
fn _f2(x: &String) -> &str {
|
||||
&**{ x }
|
||||
}
|
||||
|
||||
fn _f3(x: &Box<Box<Box<i32>>>) -> &Box<i32> {
|
||||
&***x
|
||||
}
|
||||
|
||||
fn _f4(
|
||||
x: String,
|
||||
f1: impl Fn(&str),
|
||||
f2: &dyn Fn(&str),
|
||||
f3: fn(&str),
|
||||
f4: impl CallableStr,
|
||||
f5: <() as CallableStr>::T,
|
||||
f6: <i32 as CallableStr>::T,
|
||||
f7: &dyn CallableStr<T = fn(&str)>,
|
||||
f8: impl CallableT<str>,
|
||||
f9: <() as CallableT<str>>::T,
|
||||
f10: <i32 as CallableT<str>>::T,
|
||||
f11: &dyn CallableT<str, T = fn(&str)>,
|
||||
) {
|
||||
f1(&*x);
|
||||
f2(&*x);
|
||||
f3(&*x);
|
||||
f4.callable_str()(&*x);
|
||||
f5(&*x);
|
||||
f6(&*x);
|
||||
f7.callable_str()(&*x);
|
||||
f8.callable_t()(&*x);
|
||||
f9(&*x);
|
||||
f10(&*x);
|
||||
f11.callable_t()(&*x);
|
||||
}
|
||||
|
||||
struct S1<'a>(&'a str);
|
||||
let _ = S1(&*s);
|
||||
|
||||
struct S2<'a> {
|
||||
s: &'a str,
|
||||
}
|
||||
let _ = S2 { s: &*s };
|
||||
|
||||
struct S3<'a, T: ?Sized>(&'a T);
|
||||
let _ = S3(&*s); // Don't lint. Inferred type would change.
|
||||
|
||||
struct S4<'a, T: ?Sized> {
|
||||
s: &'a T,
|
||||
}
|
||||
let _ = S4 { s: &*s }; // Don't lint. Inferred type would change.
|
||||
|
||||
enum E1<'a> {
|
||||
S1(&'a str),
|
||||
S2 { s: &'a str },
|
||||
}
|
||||
impl<'a> E1<'a> {
|
||||
fn m1(s: &'a String) {
|
||||
let _ = Self::S1(&**s);
|
||||
let _ = Self::S2 { s: &**s };
|
||||
}
|
||||
}
|
||||
let _ = E1::S1(&*s);
|
||||
let _ = E1::S2 { s: &*s };
|
||||
|
||||
enum E2<'a, T: ?Sized> {
|
||||
S1(&'a T),
|
||||
S2 { s: &'a T },
|
||||
}
|
||||
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
|
||||
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
|
||||
}
|
160
tests/ui/explicit_auto_deref.stderr
Normal file
160
tests/ui/explicit_auto_deref.stderr
Normal file
@ -0,0 +1,160 @@
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:61:20
|
||||
|
|
||||
LL | let _: &str = &*s;
|
||||
| ^^ help: try this: `s`
|
||||
|
|
||||
= note: `-D clippy::explicit-auto-deref` implied by `-D warnings`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:65:12
|
||||
|
|
||||
LL | f_str(&*s);
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:69:14
|
||||
|
|
||||
LL | f_str_t(&*s, &*s); // Don't lint second param.
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:72:25
|
||||
|
|
||||
LL | let _: &Box<i32> = &**b;
|
||||
| ^^^ help: try this: `b`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:78:8
|
||||
|
|
||||
LL | c(&*s);
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:84:9
|
||||
|
|
||||
LL | &**x
|
||||
| ^^^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:88:11
|
||||
|
|
||||
LL | { &**x }
|
||||
| ^^^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:92:9
|
||||
|
|
||||
LL | &**{ x }
|
||||
| ^^^^^^^^ help: try this: `{ x }`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:96:9
|
||||
|
|
||||
LL | &***x
|
||||
| ^^^^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:113:13
|
||||
|
|
||||
LL | f1(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:114:13
|
||||
|
|
||||
LL | f2(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:115:13
|
||||
|
|
||||
LL | f3(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:116:28
|
||||
|
|
||||
LL | f4.callable_str()(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:117:13
|
||||
|
|
||||
LL | f5(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:118:13
|
||||
|
|
||||
LL | f6(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:119:28
|
||||
|
|
||||
LL | f7.callable_str()(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:120:26
|
||||
|
|
||||
LL | f8.callable_t()(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:121:13
|
||||
|
|
||||
LL | f9(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:122:14
|
||||
|
|
||||
LL | f10(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:123:27
|
||||
|
|
||||
LL | f11.callable_t()(&*x);
|
||||
| ^^ help: try this: `x`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:127:17
|
||||
|
|
||||
LL | let _ = S1(&*s);
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:132:22
|
||||
|
|
||||
LL | let _ = S2 { s: &*s };
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:148:30
|
||||
|
|
||||
LL | let _ = Self::S1(&**s);
|
||||
| ^^^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:149:35
|
||||
|
|
||||
LL | let _ = Self::S2 { s: &**s };
|
||||
| ^^^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:152:21
|
||||
|
|
||||
LL | let _ = E1::S1(&*s);
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: deref which would be done by auto-deref
|
||||
--> $DIR/explicit_auto_deref.rs:153:26
|
||||
|
|
||||
LL | let _ = E1::S2 { s: &*s };
|
||||
| ^^ help: try this: `s`
|
||||
|
||||
error: aborting due to 26 previous errors
|
||||
|
@ -4,7 +4,8 @@
|
||||
unused_variables,
|
||||
clippy::clone_double_ref,
|
||||
clippy::needless_borrow,
|
||||
clippy::borrow_deref_ref
|
||||
clippy::borrow_deref_ref,
|
||||
clippy::explicit_auto_deref
|
||||
)]
|
||||
#![warn(clippy::explicit_deref_methods)]
|
||||
|
||||
|
@ -4,7 +4,8 @@
|
||||
unused_variables,
|
||||
clippy::clone_double_ref,
|
||||
clippy::needless_borrow,
|
||||
clippy::borrow_deref_ref
|
||||
clippy::borrow_deref_ref,
|
||||
clippy::explicit_auto_deref
|
||||
)]
|
||||
#![warn(clippy::explicit_deref_methods)]
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:35:19
|
||||
--> $DIR/explicit_deref_methods.rs:36:19
|
||||
|
|
||||
LL | let b: &str = a.deref();
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
@ -7,67 +7,67 @@ LL | let b: &str = a.deref();
|
||||
= note: `-D clippy::explicit-deref-methods` implied by `-D warnings`
|
||||
|
||||
error: explicit `deref_mut` method call
|
||||
--> $DIR/explicit_deref_methods.rs:37:23
|
||||
--> $DIR/explicit_deref_methods.rs:38:23
|
||||
|
|
||||
LL | let b: &mut str = a.deref_mut();
|
||||
| ^^^^^^^^^^^^^ help: try this: `&mut **a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:40:39
|
||||
--> $DIR/explicit_deref_methods.rs:41:39
|
||||
|
|
||||
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:40:50
|
||||
--> $DIR/explicit_deref_methods.rs:41:50
|
||||
|
|
||||
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:42:20
|
||||
--> $DIR/explicit_deref_methods.rs:43:20
|
||||
|
|
||||
LL | println!("{}", a.deref());
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:45:11
|
||||
--> $DIR/explicit_deref_methods.rs:46:11
|
||||
|
|
||||
LL | match a.deref() {
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:49:28
|
||||
--> $DIR/explicit_deref_methods.rs:50:28
|
||||
|
|
||||
LL | let b: String = concat(a.deref());
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:51:13
|
||||
--> $DIR/explicit_deref_methods.rs:52:13
|
||||
|
|
||||
LL | let b = just_return(a).deref();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `just_return(a)`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:53:28
|
||||
--> $DIR/explicit_deref_methods.rs:54:28
|
||||
|
|
||||
LL | let b: String = concat(just_return(a).deref());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `just_return(a)`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:55:19
|
||||
--> $DIR/explicit_deref_methods.rs:56:19
|
||||
|
|
||||
LL | let b: &str = a.deref().deref();
|
||||
| ^^^^^^^^^^^^^^^^^ help: try this: `&**a`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:58:13
|
||||
--> $DIR/explicit_deref_methods.rs:59:13
|
||||
|
|
||||
LL | let b = opt_a.unwrap().deref();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*opt_a.unwrap()`
|
||||
|
||||
error: explicit `deref` method call
|
||||
--> $DIR/explicit_deref_methods.rs:84:31
|
||||
--> $DIR/explicit_deref_methods.rs:85:31
|
||||
|
|
||||
LL | let b: &str = expr_deref!(a.deref());
|
||||
| ^^^^^^^^^ help: try this: `&*a`
|
||||
|
@ -1,6 +1,7 @@
|
||||
// run-rustfix
|
||||
|
||||
#![deny(clippy::useless_asref)]
|
||||
#![allow(clippy::explicit_auto_deref)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
// run-rustfix
|
||||
|
||||
#![deny(clippy::useless_asref)]
|
||||
#![allow(clippy::explicit_auto_deref)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:43:18
|
||||
--> $DIR/useless_asref.rs:44:18
|
||||
|
|
||||
LL | foo_rstr(rstr.as_ref());
|
||||
| ^^^^^^^^^^^^^ help: try this: `rstr`
|
||||
@ -11,61 +11,61 @@ LL | #![deny(clippy::useless_asref)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:45:20
|
||||
--> $DIR/useless_asref.rs:46:20
|
||||
|
|
||||
LL | foo_rslice(rslice.as_ref());
|
||||
| ^^^^^^^^^^^^^^^ help: try this: `rslice`
|
||||
|
||||
error: this call to `as_mut` does nothing
|
||||
--> $DIR/useless_asref.rs:49:21
|
||||
--> $DIR/useless_asref.rs:50:21
|
||||
|
|
||||
LL | foo_mrslice(mrslice.as_mut());
|
||||
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:51:20
|
||||
--> $DIR/useless_asref.rs:52:20
|
||||
|
|
||||
LL | foo_rslice(mrslice.as_ref());
|
||||
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:58:20
|
||||
--> $DIR/useless_asref.rs:59:20
|
||||
|
|
||||
LL | foo_rslice(rrrrrslice.as_ref());
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: try this: `rrrrrslice`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:60:18
|
||||
--> $DIR/useless_asref.rs:61:18
|
||||
|
|
||||
LL | foo_rstr(rrrrrstr.as_ref());
|
||||
| ^^^^^^^^^^^^^^^^^ help: try this: `rrrrrstr`
|
||||
|
||||
error: this call to `as_mut` does nothing
|
||||
--> $DIR/useless_asref.rs:65:21
|
||||
--> $DIR/useless_asref.rs:66:21
|
||||
|
|
||||
LL | foo_mrslice(mrrrrrslice.as_mut());
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:67:20
|
||||
--> $DIR/useless_asref.rs:68:20
|
||||
|
|
||||
LL | foo_rslice(mrrrrrslice.as_ref());
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:71:16
|
||||
--> $DIR/useless_asref.rs:72:16
|
||||
|
|
||||
LL | foo_rrrrmr((&&&&MoreRef).as_ref());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&&&&MoreRef)`
|
||||
|
||||
error: this call to `as_mut` does nothing
|
||||
--> $DIR/useless_asref.rs:121:13
|
||||
--> $DIR/useless_asref.rs:122:13
|
||||
|
|
||||
LL | foo_mrt(mrt.as_mut());
|
||||
| ^^^^^^^^^^^^ help: try this: `mrt`
|
||||
|
||||
error: this call to `as_ref` does nothing
|
||||
--> $DIR/useless_asref.rs:123:12
|
||||
--> $DIR/useless_asref.rs:124:12
|
||||
|
|
||||
LL | foo_rt(mrt.as_ref());
|
||||
| ^^^^^^^^^^^^ help: try this: `mrt`
|
||||
|
Loading…
x
Reference in New Issue
Block a user