@@ -5,10 +5,7 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
5
5
use rustc_ast as ast;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def:: Res ;
8
- use rustc_hir:: {
9
- GenericArg , HirId , Item , ItemKind , MutTy , Mutability , Node , Path , PathSegment , QPath , Ty ,
10
- TyKind ,
11
- } ;
8
+ use rustc_hir:: * ;
12
9
use rustc_middle:: ty;
13
10
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
14
11
use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
@@ -51,6 +48,45 @@ impl LateLintPass<'_> for DefaultHashTypes {
51
48
}
52
49
}
53
50
51
+ declare_tool_lint ! {
52
+ pub rustc:: POTENTIAL_QUERY_INSTABILITY ,
53
+ Allow ,
54
+ "require explicit opt-in when using potentially unstable methods or functions" ,
55
+ report_in_external_macro: true
56
+ }
57
+
58
+ declare_lint_pass ! ( QueryStability => [ POTENTIAL_QUERY_INSTABILITY ] ) ;
59
+
60
+ impl LateLintPass < ' _ > for QueryStability {
61
+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
62
+ let ( def_id, span) = match expr. kind {
63
+ ExprKind :: Path ( ref path) if let Some ( def_id) = cx. qpath_res ( path, expr. hir_id ) . opt_def_id ( ) => {
64
+ ( def_id, expr. span )
65
+ }
66
+ ExprKind :: MethodCall ( _, span, _, _) if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) => {
67
+ ( def_id, span)
68
+ } ,
69
+ _ => return ,
70
+ } ;
71
+
72
+ let substs = cx. typeck_results ( ) . node_substs ( expr. hir_id ) ;
73
+ if let Ok ( Some ( instance) ) = ty:: Instance :: resolve ( cx. tcx , cx. param_env , def_id, substs) {
74
+ let def_id = instance. def_id ( ) ;
75
+ if cx. tcx . has_attr ( def_id, sym:: rustc_lint_query_instability) {
76
+ cx. struct_span_lint ( POTENTIAL_QUERY_INSTABILITY , span, |lint| {
77
+ let msg = format ! (
78
+ "using `{}` can result in unstable query results" ,
79
+ cx. tcx. item_name( def_id)
80
+ ) ;
81
+ lint. build ( & msg)
82
+ . note ( "if you believe this case to be fine, allow this lint and add a comment explaining your rationale" )
83
+ . emit ( ) ;
84
+ } )
85
+ }
86
+ }
87
+ }
88
+ }
89
+
54
90
declare_tool_lint ! {
55
91
pub rustc:: USAGE_OF_TY_TYKIND ,
56
92
Allow ,
0 commit comments