@@ -6,7 +6,7 @@ use crate::utils::{is_entrypoint_fn, span_lint};
66use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
77use rustc:: { declare_tool_lint, lint_array} ;
88use rustc_mir:: transform:: qualify_min_const_fn:: is_min_const_fn;
9- use syntax:: ast:: { Attribute , NodeId } ;
9+ use syntax:: ast:: NodeId ;
1010use syntax_pos:: Span ;
1111
1212/// **What it does:**
@@ -78,25 +78,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
7878 span : Span ,
7979 node_id : NodeId ,
8080 ) {
81+ let def_id = cx. tcx . hir ( ) . local_def_id ( node_id) ;
82+
83+ if is_entrypoint_fn ( cx, def_id) {
84+ return ;
85+ }
86+
8187 // Perform some preliminary checks that rule out constness on the Clippy side. This way we
8288 // can skip the actual const check and return early.
8389 match kind {
84- FnKind :: ItemFn ( name , _generics , header, _vis , attrs ) => {
85- if ! can_be_const_fn ( & name . as_str ( ) , header, attrs ) {
90+ FnKind :: ItemFn ( _ , _ , header, .. ) => {
91+ if already_const ( header) {
8692 return ;
8793 }
8894 } ,
89- FnKind :: Method ( ident, sig, _vis, attrs) => {
90- let header = sig. header ;
91- let name = ident. name . as_str ( ) ;
92- if !can_be_const_fn ( & name, header, attrs) {
95+ FnKind :: Method ( _, sig, ..) => {
96+ if already_const ( sig. header ) {
9397 return ;
9498 }
9599 } ,
96100 _ => return ,
97101 }
98102
99- let def_id = cx. tcx . hir ( ) . local_def_id ( node_id) ;
100103 let mir = cx. tcx . optimized_mir ( def_id) ;
101104
102105 if let Err ( ( span, err) ) = is_min_const_fn ( cx. tcx , def_id, & mir) {
@@ -109,15 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
109112 }
110113}
111114
112- fn can_be_const_fn ( name : & str , header : hir:: FnHeader , attrs : & [ Attribute ] ) -> bool {
113- // Main and custom entrypoints can't be `const`
114- if is_entrypoint_fn ( name, attrs) {
115- return false ;
116- }
117-
118- // We don't have to lint on something that's already `const`
119- if header. constness == Constness :: Const {
120- return false ;
121- }
122- true
115+ // We don't have to lint on something that's already `const`
116+ fn already_const ( header : hir:: FnHeader ) -> bool {
117+ header. constness == Constness :: Const
123118}
0 commit comments