Skip to content

Commit

Permalink
Auto merge of rust-lang#10375 - samueltardieu:issue-10371, r=Manishearth
Browse files Browse the repository at this point in the history
Do not suggest using Self in const generic parameters

Fixes rust-lang#10371

changelog: FP: [`use_self`]: do not suggest using `Self` in const generic parameters
  • Loading branch information
bors committed Feb 26, 2023
2 parents 149392b + f531abc commit e1ac133
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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!()
}
}
}

0 comments on commit e1ac133

Please sign in to comment.