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
15 changes: 11 additions & 4 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rustc_hir::{
def::{CtorOf, DefKind, Res},
def_id::LocalDefId,
intravisit::{walk_inf, walk_ty, Visitor},
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatKind, Path, QPath,
TyKind,
Expr, ExprKind, FnRetTy, FnSig, GenericArg, GenericParam, GenericParamKind, HirId, Impl, ImplItemKind, Item,
ItemKind, Pat, PatKind, Path, QPath, Ty, TyKind,
};
use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
// avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
// we're in an `impl` or nested item, that we don't want to lint
let stack_item = if_chain! {
if let ItemKind::Impl(Impl { self_ty, .. }) = item.kind;
if let ItemKind::Impl(Impl { self_ty, generics,.. }) = item.kind;
if let TyKind::Path(QPath::Resolved(_, item_path)) = self_ty.kind;
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
if parameters.as_ref().map_or(true, |params| {
Expand All @@ -105,10 +105,17 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
if !item.span.from_expansion();
if !is_from_proc_macro(cx, item); // expensive, should be last check
then {
// Self cannot be used inside const generic parameters
let types_to_skip = generics.params.iter().filter_map(|param| {
match param {
GenericParam { kind: GenericParamKind::Const { ty: Ty { hir_id, ..}, ..}, ..} => Some(*hir_id),
_ => None,
}
}).chain(std::iter::once(self_ty.hir_id)).collect();
StackItem::Check {
impl_id: item.owner_id.def_id,
in_body: 0,
types_to_skip: std::iter::once(self_ty.hir_id).collect(),
types_to_skip,
}
} else {
StackItem::NoCheck
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,13 @@ fn msrv_1_37() {
}
}
}

mod issue_10371 {
struct Val<const V: i32> {}

impl<const V: i32> From<Val<V>> for i32 {
fn from(_: Val<V>) -> Self {
todo!()
}
}
}
10 changes: 10 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,13 @@ fn msrv_1_37() {
}
}
}

mod issue_10371 {
struct Val<const V: i32> {}

impl<const V: i32> From<Val<V>> for i32 {
fn from(_: Val<V>) -> Self {
todo!()
}
}
}