Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8286824

Browse files
committed
Add inferred args to typeck
1 parent 00faed9 commit 8286824

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

clippy_lints/src/implicit_hasher.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::BTreeMap;
55

66
use rustc_errors::DiagnosticBuilder;
77
use rustc_hir as hir;
8-
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, NestedVisitorMap, Visitor};
8+
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, walk_inf, NestedVisitorMap, Visitor};
99
use rustc_hir::{Body, Expr, ExprKind, GenericArg, Item, ItemKind, QPath, TyKind};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::hir::map::Map;
@@ -295,6 +295,14 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplicitHasherTypeVisitor<'a, 'tcx> {
295295
walk_ty(self, t);
296296
}
297297

298+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
299+
if let Some(target) = ImplicitHasherType::new(self.cx, &inf.to_ty()) {
300+
self.found.push(target);
301+
}
302+
303+
walk_inf(self, inf);
304+
}
305+
298306
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
299307
NestedVisitorMap::None
300308
}

clippy_lints/src/types/type_complexity.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use rustc_hir as hir;
3-
use rustc_hir::intravisit::{walk_ty, NestedVisitorMap, Visitor};
3+
use rustc_hir::intravisit::{walk_ty, walk_inf, NestedVisitorMap, Visitor};
44
use rustc_hir::{GenericParamKind, TyKind};
55
use rustc_lint::LateContext;
66
use rustc_middle::hir::map::Map;
@@ -39,6 +39,11 @@ struct TypeComplexityVisitor {
3939
impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
4040
type Map = Map<'tcx>;
4141

42+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
43+
self.score += 1;
44+
walk_inf(self, inf);
45+
}
46+
4247
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_>) {
4348
let (add_score, sub_nest) = match ty.kind {
4449
// _, &x and *x have only small overhead; don't mess with nesting level

clippy_lints/src/use_self.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rustc_hir::{
88
self as hir,
99
def::{CtorOf, DefKind, Res},
1010
def_id::LocalDefId,
11-
intravisit::{walk_ty, NestedVisitorMap, Visitor},
12-
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Path, QPath, TyKind,
11+
intravisit::{walk_ty, walk_inf, NestedVisitorMap, Visitor},
12+
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Node, Path, PathSegment,
13+
QPath, TyKind,
1314
};
1415
use rustc_lint::{LateContext, LateLintPass, LintContext};
1516
use rustc_middle::hir::map::Map;
@@ -263,6 +264,11 @@ struct SkipTyCollector {
263264
impl<'tcx> Visitor<'tcx> for SkipTyCollector {
264265
type Map = Map<'tcx>;
265266

267+
fn visit_infer(&mut self, inf: &hir::InferArg) {
268+
self.types_to_skip.push(inf.hir_id);
269+
270+
walk_inf(self, inf)
271+
}
266272
fn visit_ty(&mut self, hir_ty: &hir::Ty<'_>) {
267273
self.types_to_skip.push(hir_ty.hir_id);
268274

@@ -274,6 +280,52 @@ impl<'tcx> Visitor<'tcx> for SkipTyCollector {
274280
}
275281
}
276282

283+
<<<<<<< HEAD
284+
=======
285+
struct LintTyCollector<'a, 'tcx> {
286+
cx: &'a LateContext<'tcx>,
287+
self_ty: Ty<'tcx>,
288+
types_to_lint: Vec<HirId>,
289+
types_to_skip: Vec<HirId>,
290+
}
291+
292+
impl<'a, 'tcx> Visitor<'tcx> for LintTyCollector<'a, 'tcx> {
293+
type Map = Map<'tcx>;
294+
295+
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'_>) {
296+
if_chain! {
297+
if let Some(ty) = self.cx.typeck_results().node_type_opt(hir_ty.hir_id);
298+
if should_lint_ty(hir_ty, ty, self.self_ty);
299+
then {
300+
self.types_to_lint.push(hir_ty.hir_id);
301+
} else {
302+
self.types_to_skip.push(hir_ty.hir_id);
303+
}
304+
}
305+
306+
walk_ty(self, hir_ty);
307+
}
308+
309+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
310+
if_chain! {
311+
if let Some(ty) = self.cx.typeck_results().node_type_opt(inf.hir_id);
312+
if should_lint_ty(&inf.to_ty(), ty, self.self_ty);
313+
then {
314+
self.types_to_lint.push(inf.hir_id);
315+
} else {
316+
self.types_to_skip.push(inf.hir_id);
317+
}
318+
}
319+
320+
walk_inf(self, inf)
321+
}
322+
323+
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
324+
NestedVisitorMap::None
325+
}
326+
}
327+
328+
>>>>>>> Add inferred args to typeck
277329
fn span_lint(cx: &LateContext<'_>, span: Span) {
278330
span_lint_and_sugg(
279331
cx,

clippy_utils/src/hir_utils.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ impl HirEqInterExpr<'_, '_, '_> {
288288
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
289289
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
290290
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
291+
(GenericArg::Infer(l_inf), GenericArg::Infer(r_inf)) =>
292+
self.eq_ty(&l_inf.to_ty(), &r_inf.to_ty()),
291293
_ => false,
292294
}
293295
}
@@ -888,10 +890,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
888890
self.hash_tykind(&ty.kind);
889891
}
890892

891-
pub fn hash_infer(&mut self) {
892-
"_".hash(&mut self.s);
893-
}
894-
895893
pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
896894
match ty {
897895
TyKind::Slice(ty) => {
@@ -957,7 +955,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
957955
GenericArg::Lifetime(l) => self.hash_lifetime(l),
958956
GenericArg::Type(ref ty) => self.hash_ty(ty),
959957
GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
960-
GenericArg::Infer(ref _inf) => self.hash_infer(),
958+
GenericArg::Infer(ref inf) => self.hash_ty(&inf.to_ty()),
961959
}
962960
}
963961
}

clippy_utils/src/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
180180
}
181181

182182
// FIXME: Per https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/infer/at/struct.At.html#method.normalize
183-
// this function can be removed once the `normalizie` method does not panic when normalization does
183+
// this function can be removed once the `normalize` method does not panic when normalization does
184184
// not succeed
185185
/// Checks if `Ty` is normalizable. This function is useful
186186
/// to avoid crashes on `layout_of`.

tests/ui/transmute_ptr_to_ref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ error: transmute from a pointer type (`*const i32`) to a reference type (`&issue
4646
--> $DIR/transmute_ptr_to_ref.rs:32:32
4747
|
4848
LL | let _: &Foo<u8> = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) };
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const Foo<_>)`
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const issue1231::Foo<u8>)`
5050

5151
error: transmute from a pointer type (`*const i32`) to a reference type (`&issue1231::Foo<&u8>`)
5252
--> $DIR/transmute_ptr_to_ref.rs:34:33
5353
|
5454
LL | let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) };
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const Foo<&_>)`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const issue1231::Foo<&u8>)`
5656

5757
error: transmute from a pointer type (`*const i32`) to a reference type (`&u8`)
5858
--> $DIR/transmute_ptr_to_ref.rs:38:14

0 commit comments

Comments
 (0)