File tree 3 files changed +50
-4
lines changed
3 files changed +50
-4
lines changed Original file line number Diff line number Diff line change @@ -2957,17 +2957,19 @@ impl<'tcx> LateLintPass<'tcx> for FunctionReferences {
2957
2957
if let hir:: ExprKind :: Path ( qpath) = & referent. kind {
2958
2958
if let Some ( def_id) = cx. qpath_res ( qpath, referent. hir_id ) . opt_def_id ( ) {
2959
2959
cx. tcx . hir ( ) . get_if_local ( def_id) . map ( |node| {
2960
- if node. fn_decl ( ) . is_some ( ) {
2960
+ node. fn_decl ( ) . map ( |decl| {
2961
2961
if let Some ( ident) = node. ident ( ) {
2962
2962
cx. struct_span_lint ( FUNCTION_REFERENCES , referent. span , |lint| {
2963
+ let num_args = decl. inputs . len ( ) ;
2963
2964
lint. build ( & format ! (
2964
- "cast {} with `as *const ()` to use it as a pointer" ,
2965
- ident. to_string( )
2965
+ "cast `{}` with `as *const fn({}) -> _` to use it as a pointer" ,
2966
+ ident. to_string( ) ,
2967
+ vec![ "_" ; num_args] . join( ", " )
2966
2968
) )
2967
2969
. emit ( )
2968
2970
} ) ;
2969
2971
}
2970
- }
2972
+ } ) ;
2971
2973
} ) ;
2972
2974
}
2973
2975
}
Original file line number Diff line number Diff line change
1
+ // check-pass
2
+ fn foo ( ) -> usize { 42 }
3
+ fn bar ( x : usize ) -> usize { x }
4
+ fn baz ( x : usize , y : usize ) -> usize { x + y }
5
+
6
+ fn main ( ) {
7
+ println ! ( "{:p}" , & foo) ;
8
+ //~^ WARN cast `foo` with `as *const fn() -> _` to use it as a pointer
9
+ println ! ( "{:p}" , & bar) ;
10
+ //~^ WARN cast `bar` with `as *const fn(_) -> _` to use it as a pointer
11
+ println ! ( "{:p}" , & baz) ;
12
+ //~^ WARN cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer
13
+
14
+ //should not produce any warnings
15
+ println ! ( "{:p}" , foo as * const fn ( ) -> usize ) ;
16
+ println ! ( "{:p}" , bar as * const fn ( usize ) -> usize ) ;
17
+ println ! ( "{:p}" , baz as * const fn ( usize , usize ) -> usize ) ;
18
+
19
+ //should not produce any warnings
20
+ let fn_thing = foo;
21
+ println ! ( "{:p}" , & fn_thing) ;
22
+ }
Original file line number Diff line number Diff line change
1
+ warning: cast `foo` with `as *const fn() -> _` to use it as a pointer
2
+ --> $DIR/function-references.rs:7:23
3
+ |
4
+ LL | println!("{:p}", &foo);
5
+ | ^^^
6
+ |
7
+ = note: `#[warn(function_references)]` on by default
8
+
9
+ warning: cast `bar` with `as *const fn(_) -> _` to use it as a pointer
10
+ --> $DIR/function-references.rs:9:23
11
+ |
12
+ LL | println!("{:p}", &bar);
13
+ | ^^^
14
+
15
+ warning: cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer
16
+ --> $DIR/function-references.rs:11:23
17
+ |
18
+ LL | println!("{:p}", &baz);
19
+ | ^^^
20
+
21
+ warning: 3 warnings emitted
22
+
You can’t perform that action at this time.
0 commit comments