@@ -5,9 +5,12 @@ use rustc_lint::{LateContext, LateLintPass};
55use rustc_session:: impl_lint_pass;
66use rustc_span:: { FileName , Span , sym} ;
77
8+ use clippy_config:: Conf ;
89use clippy_utils:: diagnostics:: span_lint_and_then;
910use clippy_utils:: macros:: root_macro_call_first_node;
1011
12+ use cargo_metadata:: MetadataCommand ;
13+
1114use std:: path:: { Path , PathBuf } ;
1215
1316declare_clippy_lint ! {
@@ -42,15 +45,34 @@ declare_clippy_lint! {
4245pub ( crate ) struct IncludeFileOutsideProject {
4346 cargo_manifest_dir : Option < PathBuf > ,
4447 warned_spans : FxHashSet < PathBuf > ,
48+ can_check_crate : bool ,
4549}
4650
4751impl_lint_pass ! ( IncludeFileOutsideProject => [ INCLUDE_FILE_OUTSIDE_PROJECT ] ) ;
4852
4953impl IncludeFileOutsideProject {
50- pub ( crate ) fn new ( ) -> Self {
54+ pub ( crate ) fn new ( conf : & ' static Conf ) -> Self {
55+ let mut can_check_crate = true ;
56+ if !conf. cargo_ignore_publish {
57+ match MetadataCommand :: new ( ) . no_deps ( ) . exec ( ) {
58+ Ok ( metadata) => {
59+ for package in & metadata. packages {
60+ // only run the lint if publish is `None` (`publish = true` or skipped entirely)
61+ // or if the vector isn't empty (`publish = ["something"]`)
62+ if package. publish . as_ref ( ) . filter ( |publish| publish. is_empty ( ) ) . is_some ( ) {
63+ can_check_crate = false ;
64+ break ;
65+ }
66+ }
67+ } ,
68+ Err ( _) => can_check_crate = false ,
69+ }
70+ }
71+
5172 Self {
52- cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( |dir| PathBuf :: from ( dir ) ) ,
73+ cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( PathBuf :: from) ,
5374 warned_spans : FxHashSet :: default ( ) ,
75+ can_check_crate,
5476 }
5577 }
5678
@@ -72,12 +94,12 @@ impl IncludeFileOutsideProject {
7294 }
7395 }
7496
75- fn is_part_of_project_dir ( & self , file_path : & PathBuf ) -> bool {
97+ fn is_part_of_project_dir ( & self , file_path : & Path ) -> bool {
7698 if let Some ( ref cargo_manifest_dir) = self . cargo_manifest_dir {
7799 // Check if both paths start with the same thing.
78100 let mut file_iter = file_path. iter ( ) ;
79101
80- for cargo_item in cargo_manifest_dir. iter ( ) {
102+ for cargo_item in cargo_manifest_dir {
81103 match file_iter. next ( ) {
82104 Some ( file_path) if file_path == cargo_item => { } ,
83105 _ => {
@@ -141,6 +163,9 @@ impl IncludeFileOutsideProject {
141163
142164impl LateLintPass < ' _ > for IncludeFileOutsideProject {
143165 fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
166+ if !self . can_check_crate {
167+ return ;
168+ }
144169 if !expr. span . from_expansion ( ) {
145170 self . check_hir_id ( cx, expr. span , expr. hir_id ) ;
146171 } else if let ExprKind :: Lit ( lit) = & expr. kind
@@ -156,12 +181,15 @@ impl LateLintPass<'_> for IncludeFileOutsideProject {
156181 fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & ' _ Item < ' _ > ) {
157182 // Interestingly enough, `include!` content is not considered expanded. Which allows us
158183 // to easily filter out items we're not interested into.
159- if !item. span . from_expansion ( ) {
184+ if self . can_check_crate && !item. span . from_expansion ( ) {
160185 self . check_hir_id ( cx, item. span , item. hir_id ( ) ) ;
161186 }
162187 }
163188
164189 fn check_attributes ( & mut self , cx : & LateContext < ' _ > , attrs : & [ Attribute ] ) {
190+ if !self . can_check_crate {
191+ return ;
192+ }
165193 for attr in attrs {
166194 if let Some ( attr) = attr. meta ( ) {
167195 self . check_attribute ( cx, & attr) ;
0 commit comments