1
1
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
2
2
//! Clippy.
3
3
4
- use rustc_ast as ast ;
4
+ use rustc_hir :: HirId ;
5
5
use rustc_hir:: def:: Res ;
6
6
use rustc_hir:: def_id:: DefId ;
7
- use rustc_hir:: {
8
- AmbigArg , BinOp , BinOpKind , Expr , ExprKind , GenericArg , HirId , Impl , Item , ItemKind , Node , Pat ,
9
- PatExpr , PatExprKind , PatKind , Path , PathSegment , QPath , Ty , TyKind ,
10
- } ;
11
7
use rustc_middle:: ty:: { self , GenericArgsRef , Ty as MiddleTy } ;
12
8
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
13
9
use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
14
10
use rustc_span:: { Span , sym} ;
15
11
use tracing:: debug;
12
+ use { rustc_ast as ast, rustc_hir as hir} ;
16
13
17
14
use crate :: lints:: {
18
15
BadOptAccessDiag , DefaultHashTypesDiag , DiagOutOfImpl , LintPassByHand ,
@@ -37,9 +34,12 @@ declare_tool_lint! {
37
34
declare_lint_pass ! ( DefaultHashTypes => [ DEFAULT_HASH_TYPES ] ) ;
38
35
39
36
impl LateLintPass < ' _ > for DefaultHashTypes {
40
- fn check_path ( & mut self , cx : & LateContext < ' _ > , path : & Path < ' _ > , hir_id : HirId ) {
37
+ fn check_path ( & mut self , cx : & LateContext < ' _ > , path : & hir :: Path < ' _ > , hir_id : HirId ) {
41
38
let Res :: Def ( rustc_hir:: def:: DefKind :: Struct , def_id) = path. res else { return } ;
42
- if matches ! ( cx. tcx. hir_node( hir_id) , Node :: Item ( Item { kind: ItemKind :: Use ( ..) , .. } ) ) {
39
+ if matches ! (
40
+ cx. tcx. hir_node( hir_id) ,
41
+ hir:: Node :: Item ( hir:: Item { kind: hir:: ItemKind :: Use ( ..) , .. } )
42
+ ) {
43
43
// Don't lint imports, only actual usages.
44
44
return ;
45
45
}
@@ -60,10 +60,10 @@ impl LateLintPass<'_> for DefaultHashTypes {
60
60
/// get the `DefId` and `GenericArgsRef` of the function.
61
61
fn typeck_results_of_method_fn < ' tcx > (
62
62
cx : & LateContext < ' tcx > ,
63
- expr : & Expr < ' _ > ,
63
+ expr : & hir :: Expr < ' _ > ,
64
64
) -> Option < ( Span , DefId , ty:: GenericArgsRef < ' tcx > ) > {
65
65
match expr. kind {
66
- ExprKind :: MethodCall ( segment, ..)
66
+ hir :: ExprKind :: MethodCall ( segment, ..)
67
67
if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) =>
68
68
{
69
69
Some ( ( segment. ident . span , def_id, cx. typeck_results ( ) . node_args ( expr. hir_id ) ) )
@@ -102,7 +102,7 @@ declare_tool_lint! {
102
102
declare_lint_pass ! ( QueryStability => [ POTENTIAL_QUERY_INSTABILITY , UNTRACKED_QUERY_INFORMATION ] ) ;
103
103
104
104
impl LateLintPass < ' _ > for QueryStability {
105
- fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
105
+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & hir :: Expr < ' _ > ) {
106
106
let Some ( ( span, def_id, args) ) = typeck_results_of_method_fn ( cx, expr) else { return } ;
107
107
if let Ok ( Some ( instance) ) = ty:: Instance :: try_resolve ( cx. tcx , cx. typing_env ( ) , def_id, args)
108
108
{
@@ -164,21 +164,25 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
164
164
}
165
165
}
166
166
167
- fn check_ty ( & mut self , cx : & LateContext < ' _ > , ty : & ' tcx Ty < ' tcx , AmbigArg > ) {
167
+ fn check_ty ( & mut self , cx : & LateContext < ' _ > , ty : & ' tcx hir :: Ty < ' tcx , hir :: AmbigArg > ) {
168
168
match & ty. kind {
169
- TyKind :: Path ( QPath :: Resolved ( _, path) ) => {
169
+ hir :: TyKind :: Path ( hir :: QPath :: Resolved ( _, path) ) => {
170
170
if lint_ty_kind_usage ( cx, & path. res ) {
171
171
let span = match cx. tcx . parent_hir_node ( ty. hir_id ) {
172
- Node :: PatExpr ( PatExpr { kind : PatExprKind :: Path ( qpath) , .. } )
173
- | Node :: Pat ( Pat {
174
- kind : PatKind :: TupleStruct ( qpath, ..) | PatKind :: Struct ( qpath, ..) ,
172
+ hir:: Node :: PatExpr ( hir:: PatExpr {
173
+ kind : hir:: PatExprKind :: Path ( qpath) ,
174
+ ..
175
+ } )
176
+ | hir:: Node :: Pat ( hir:: Pat {
177
+ kind :
178
+ hir:: PatKind :: TupleStruct ( qpath, ..) | hir:: PatKind :: Struct ( qpath, ..) ,
175
179
..
176
180
} )
177
- | Node :: Expr (
178
- Expr { kind : ExprKind :: Path ( qpath) , .. }
179
- | & Expr { kind : ExprKind :: Struct ( qpath, ..) , .. } ,
181
+ | hir :: Node :: Expr (
182
+ hir :: Expr { kind : hir :: ExprKind :: Path ( qpath) , .. }
183
+ | & hir :: Expr { kind : hir :: ExprKind :: Struct ( qpath, ..) , .. } ,
180
184
) => {
181
- if let QPath :: TypeRelative ( qpath_ty, ..) = qpath
185
+ if let hir :: QPath :: TypeRelative ( qpath_ty, ..) = qpath
182
186
&& qpath_ty. hir_id == ty. hir_id
183
187
{
184
188
Some ( path. span )
@@ -223,7 +227,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_>, res: &Res) -> bool {
223
227
}
224
228
}
225
229
226
- fn is_ty_or_ty_ctxt ( cx : & LateContext < ' _ > , path : & Path < ' _ > ) -> Option < String > {
230
+ fn is_ty_or_ty_ctxt ( cx : & LateContext < ' _ > , path : & hir :: Path < ' _ > ) -> Option < String > {
227
231
match & path. res {
228
232
Res :: Def ( _, def_id) => {
229
233
if let Some ( name @ ( sym:: Ty | sym:: TyCtxt ) ) = cx. tcx . get_diagnostic_name ( * def_id) {
@@ -244,13 +248,17 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
244
248
None
245
249
}
246
250
247
- fn gen_args ( segment : & PathSegment < ' _ > ) -> String {
251
+ fn gen_args ( segment : & hir :: PathSegment < ' _ > ) -> String {
248
252
if let Some ( args) = & segment. args {
249
253
let lifetimes = args
250
254
. args
251
255
. iter ( )
252
256
. filter_map ( |arg| {
253
- if let GenericArg :: Lifetime ( lt) = arg { Some ( lt. ident . to_string ( ) ) } else { None }
257
+ if let hir:: GenericArg :: Lifetime ( lt) = arg {
258
+ Some ( lt. ident . to_string ( ) )
259
+ } else {
260
+ None
261
+ }
254
262
} )
255
263
. collect :: < Vec < _ > > ( ) ;
256
264
@@ -284,7 +292,7 @@ declare_tool_lint! {
284
292
declare_lint_pass ! ( TypeIr => [ NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT , USAGE_OF_TYPE_IR_INHERENT ] ) ;
285
293
286
294
impl < ' tcx > LateLintPass < ' tcx > for TypeIr {
287
- fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
295
+ fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx hir :: Item < ' tcx > ) {
288
296
let rustc_hir:: ItemKind :: Use ( path, kind) = item. kind else { return } ;
289
297
290
298
let is_mod_inherent = |def_id| cx. tcx . is_diagnostic_item ( sym:: type_ir_inherent, def_id) ;
@@ -394,23 +402,23 @@ declare_tool_lint! {
394
402
declare_lint_pass ! ( Diagnostics => [ UNTRANSLATABLE_DIAGNOSTIC , DIAGNOSTIC_OUTSIDE_OF_IMPL ] ) ;
395
403
396
404
impl LateLintPass < ' _ > for Diagnostics {
397
- fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
398
- let collect_args_tys_and_spans = |args : & [ Expr < ' _ > ] , reserve_one_extra : bool | {
405
+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & hir :: Expr < ' _ > ) {
406
+ let collect_args_tys_and_spans = |args : & [ hir :: Expr < ' _ > ] , reserve_one_extra : bool | {
399
407
let mut result = Vec :: with_capacity ( args. len ( ) + usize:: from ( reserve_one_extra) ) ;
400
408
result. extend ( args. iter ( ) . map ( |arg| ( cx. typeck_results ( ) . expr_ty ( arg) , arg. span ) ) ) ;
401
409
result
402
410
} ;
403
411
// Only check function calls and method calls.
404
412
let ( span, def_id, fn_gen_args, arg_tys_and_spans) = match expr. kind {
405
- ExprKind :: Call ( callee, args) => {
413
+ hir :: ExprKind :: Call ( callee, args) => {
406
414
match cx. typeck_results ( ) . node_type ( callee. hir_id ) . kind ( ) {
407
415
& ty:: FnDef ( def_id, fn_gen_args) => {
408
416
( callee. span , def_id, fn_gen_args, collect_args_tys_and_spans ( args, false ) )
409
417
}
410
418
_ => return , // occurs for fns passed as args
411
419
}
412
420
}
413
- ExprKind :: MethodCall ( _segment, _recv, args, _span) => {
421
+ hir :: ExprKind :: MethodCall ( _segment, _recv, args, _span) => {
414
422
let Some ( ( span, def_id, fn_gen_args) ) = typeck_results_of_method_fn ( cx, expr)
415
423
else {
416
424
return ;
@@ -514,8 +522,8 @@ impl Diagnostics {
514
522
let mut is_inside_appropriate_impl = false ;
515
523
for ( _hir_id, parent) in cx. tcx . hir_parent_iter ( current_id) {
516
524
debug ! ( ?parent) ;
517
- if let Node :: Item ( Item { kind : ItemKind :: Impl ( impl_) , .. } ) = parent
518
- && let Impl { of_trait : Some ( of_trait) , .. } = impl_
525
+ if let hir :: Node :: Item ( hir :: Item { kind : hir :: ItemKind :: Impl ( impl_) , .. } ) = parent
526
+ && let hir :: Impl { of_trait : Some ( of_trait) , .. } = impl_
519
527
&& let Some ( def_id) = of_trait. trait_def_id ( )
520
528
&& let Some ( name) = cx. tcx . get_diagnostic_name ( def_id)
521
529
&& matches ! ( name, sym:: Diagnostic | sym:: Subdiagnostic | sym:: LintDiagnostic )
@@ -543,8 +551,8 @@ declare_tool_lint! {
543
551
declare_lint_pass ! ( BadOptAccess => [ BAD_OPT_ACCESS ] ) ;
544
552
545
553
impl LateLintPass < ' _ > for BadOptAccess {
546
- fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
547
- let ExprKind :: Field ( base, target) = expr. kind else { return } ;
554
+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & hir :: Expr < ' _ > ) {
555
+ let hir :: ExprKind :: Field ( base, target) = expr. kind else { return } ;
548
556
let Some ( adt_def) = cx. typeck_results ( ) . expr_ty ( base) . ty_adt_def ( ) else { return } ;
549
557
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
550
558
// avoided.
@@ -581,9 +589,12 @@ declare_tool_lint! {
581
589
declare_lint_pass ! ( SpanUseEqCtxt => [ SPAN_USE_EQ_CTXT ] ) ;
582
590
583
591
impl < ' tcx > LateLintPass < ' tcx > for SpanUseEqCtxt {
584
- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & Expr < ' _ > ) {
585
- if let ExprKind :: Binary ( BinOp { node : BinOpKind :: Eq | BinOpKind :: Ne , .. } , lhs, rhs) =
586
- expr. kind
592
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & hir:: Expr < ' _ > ) {
593
+ if let hir:: ExprKind :: Binary (
594
+ hir:: BinOp { node : hir:: BinOpKind :: Eq | hir:: BinOpKind :: Ne , .. } ,
595
+ lhs,
596
+ rhs,
597
+ ) = expr. kind
587
598
{
588
599
if is_span_ctxt_call ( cx, lhs) && is_span_ctxt_call ( cx, rhs) {
589
600
cx. emit_span_lint ( SPAN_USE_EQ_CTXT , expr. span , SpanUseEqCtxtDiag ) ;
@@ -592,9 +603,9 @@ impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
592
603
}
593
604
}
594
605
595
- fn is_span_ctxt_call ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
606
+ fn is_span_ctxt_call ( cx : & LateContext < ' _ > , expr : & hir :: Expr < ' _ > ) -> bool {
596
607
match & expr. kind {
597
- ExprKind :: MethodCall ( ..) => cx
608
+ hir :: ExprKind :: MethodCall ( ..) => cx
598
609
. typeck_results ( )
599
610
. type_dependent_def_id ( expr. hir_id )
600
611
. is_some_and ( |call_did| cx. tcx . is_diagnostic_item ( sym:: SpanCtxt , call_did) ) ,
@@ -617,11 +628,11 @@ declare_lint_pass!(SymbolInternStringLiteral => [SYMBOL_INTERN_STRING_LITERAL]);
617
628
618
629
impl < ' tcx > LateLintPass < ' tcx > for SymbolInternStringLiteral {
619
630
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
620
- if let ExprKind :: Call ( path, [ arg] ) = expr. kind
621
- && let ExprKind :: Path ( ref qpath) = path. kind
631
+ if let hir :: ExprKind :: Call ( path, [ arg] ) = expr. kind
632
+ && let hir :: ExprKind :: Path ( ref qpath) = path. kind
622
633
&& let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( )
623
634
&& cx. tcx . is_diagnostic_item ( sym:: SymbolIntern , def_id)
624
- && let ExprKind :: Lit ( kind) = arg. kind
635
+ && let hir :: ExprKind :: Lit ( kind) = arg. kind
625
636
&& let rustc_ast:: LitKind :: Str ( _, _) = kind. node
626
637
{
627
638
cx. emit_span_lint (
0 commit comments