File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -2942,3 +2942,35 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
2942
2942
}
2943
2943
}
2944
2944
}
2945
+
2946
+ declare_lint ! {
2947
+ FUNCTION_REFERENCES ,
2948
+ Warn ,
2949
+ "suggest casting functions to pointers when attempting to take references"
2950
+ }
2951
+
2952
+ declare_lint_pass ! ( FunctionReferences => [ FUNCTION_REFERENCES ] ) ;
2953
+
2954
+ impl < ' tcx > LateLintPass < ' tcx > for FunctionReferences {
2955
+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , e : & hir:: Expr < ' _ > ) {
2956
+ if let hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Ref , _, referent) = e. kind {
2957
+ if let hir:: ExprKind :: Path ( qpath) = & referent. kind {
2958
+ if let Some ( def_id) = cx. qpath_res ( qpath, referent. hir_id ) . opt_def_id ( ) {
2959
+ cx. tcx . hir ( ) . get_if_local ( def_id) . map ( |node| {
2960
+ if node. fn_decl ( ) . is_some ( ) {
2961
+ if let Some ( ident) = node. ident ( ) {
2962
+ cx. struct_span_lint ( FUNCTION_REFERENCES , referent. span , |lint| {
2963
+ lint. build ( & format ! (
2964
+ "cast {} with `as *const ()` to use it as a pointer" ,
2965
+ ident. to_string( )
2966
+ ) )
2967
+ . emit ( )
2968
+ } ) ;
2969
+ }
2970
+ }
2971
+ } ) ;
2972
+ }
2973
+ }
2974
+ }
2975
+ }
2976
+ }
Original file line number Diff line number Diff line change @@ -194,6 +194,7 @@ macro_rules! late_lint_mod_passes {
194
194
UnreachablePub : UnreachablePub ,
195
195
ExplicitOutlivesRequirements : ExplicitOutlivesRequirements ,
196
196
InvalidValue : InvalidValue ,
197
+ FunctionReferences : FunctionReferences ,
197
198
]
198
199
) ;
199
200
} ;
You can’t perform that action at this time.
0 commit comments