@@ -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:**
@@ -82,25 +82,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
8282 span : Span ,
8383 node_id : NodeId ,
8484 ) {
85+ let def_id = cx. tcx . hir ( ) . local_def_id ( node_id) ;
86+
87+ if is_entrypoint_fn ( cx, def_id) {
88+ return ;
89+ }
90+
8591 // Perform some preliminary checks that rule out constness on the Clippy side. This way we
8692 // can skip the actual const check and return early.
8793 match kind {
88- FnKind :: ItemFn ( name , _generics , header, _vis , attrs ) => {
89- if ! can_be_const_fn ( & name . as_str ( ) , header, attrs ) {
94+ FnKind :: ItemFn ( _ , _ , header, .. ) => {
95+ if already_const ( header) {
9096 return ;
9197 }
9298 } ,
93- FnKind :: Method ( ident, sig, _vis, attrs) => {
94- let header = sig. header ;
95- let name = ident. name . as_str ( ) ;
96- if !can_be_const_fn ( & name, header, attrs) {
99+ FnKind :: Method ( _, sig, ..) => {
100+ if already_const ( sig. header ) {
97101 return ;
98102 }
99103 } ,
100104 _ => return ,
101105 }
102106
103- let def_id = cx. tcx . hir ( ) . local_def_id ( node_id) ;
104107 let mir = cx. tcx . optimized_mir ( def_id) ;
105108
106109 if let Err ( ( span, err) ) = is_min_const_fn ( cx. tcx , def_id, & mir) {
@@ -113,15 +116,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
113116 }
114117}
115118
116- fn can_be_const_fn ( name : & str , header : hir:: FnHeader , attrs : & [ Attribute ] ) -> bool {
117- // Main and custom entrypoints can't be `const`
118- if is_entrypoint_fn ( name, attrs) {
119- return false ;
120- }
121-
122- // We don't have to lint on something that's already `const`
123- if header. constness == Constness :: Const {
124- return false ;
125- }
126- true
119+ // We don't have to lint on something that's already `const`
120+ fn already_const ( header : hir:: FnHeader ) -> bool {
121+ header. constness == Constness :: Const
127122}
0 commit comments