@@ -521,6 +521,24 @@ declare_lint! {
521
521
"using `.cloned().collect()` on slice to create a `Vec`"
522
522
}
523
523
524
+ /// **What it does:** Checks for calls to the main function
525
+ ///
526
+ /// **Why is this bad?** Recursing the main function is unlikely what was intended
527
+ ///
528
+ /// **Known problems:** None.
529
+ ///
530
+ /// **Example:**
531
+ /// ```rust
532
+ /// fn foo() {
533
+ /// ::main();
534
+ /// }
535
+ /// ```
536
+ declare_lint ! {
537
+ pub CALLING_MAIN ,
538
+ Warn ,
539
+ "manually called the main function"
540
+ }
541
+
524
542
impl LintPass for Pass {
525
543
fn get_lints ( & self ) -> LintArray {
526
544
lint_array ! ( OPTION_UNWRAP_USED ,
@@ -545,14 +563,15 @@ impl LintPass for Pass {
545
563
ITER_SKIP_NEXT ,
546
564
GET_UNWRAP ,
547
565
STRING_EXTEND_CHARS ,
566
+ CALLING_MAIN ,
548
567
ITER_CLONED_COLLECT )
549
568
}
550
569
}
551
570
552
571
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for Pass {
553
572
#[ allow( unused_attributes) ]
554
573
// ^ required because `cyclomatic_complexity` attribute shows up as unused
555
- #[ cyclomatic_complexity = "30 " ]
574
+ #[ cyclomatic_complexity = "31 " ]
556
575
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx hir:: Expr ) {
557
576
if in_macro ( cx, expr. span ) {
558
577
return ;
@@ -627,6 +646,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
627
646
lint_chars_next ( cx, expr, rhs, lhs, op. node == hir:: BiEq ) ;
628
647
}
629
648
} ,
649
+ hir:: ExprCall ( ref func, ref args) => {
650
+ if args. is_empty ( ) {
651
+ // libs don't have a main
652
+ if let Some ( ( main, _) ) = * cx. sess ( ) . entry_fn . borrow ( ) {
653
+ if let hir:: ExprPath ( ref qpath) = func. node {
654
+ let main_did = cx. tcx . hir . local_def_id ( main) ;
655
+ let fun_did = cx. tables . qpath_def ( qpath, func. id ) . def_id ( ) ;
656
+ if main_did == fun_did {
657
+ span_lint ( cx, CALLING_MAIN , expr. span , "called the `main` function manually" ) ;
658
+ }
659
+ }
660
+ }
661
+ }
662
+ } ,
630
663
_ => ( ) ,
631
664
}
632
665
}
0 commit comments