@@ -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 ! {
@@ -36,15 +39,34 @@ declare_clippy_lint! {
3639pub ( crate ) struct IncludeFileOutsideProject {
3740 cargo_manifest_dir : Option < PathBuf > ,
3841 warned_spans : FxHashSet < PathBuf > ,
42+ can_check_crate : bool ,
3943}
4044
4145impl_lint_pass ! ( IncludeFileOutsideProject => [ INCLUDE_FILE_OUTSIDE_PROJECT ] ) ;
4246
4347impl IncludeFileOutsideProject {
44- pub ( crate ) fn new ( ) -> Self {
48+ pub ( crate ) fn new ( conf : & ' static Conf ) -> Self {
49+ let mut can_check_crate = true ;
50+ if !conf. cargo_ignore_publish {
51+ match MetadataCommand :: new ( ) . no_deps ( ) . exec ( ) {
52+ Ok ( metadata) => {
53+ for package in & metadata. packages {
54+ // only run the lint if publish is `None` (`publish = true` or skipped entirely)
55+ // or if the vector isn't empty (`publish = ["something"]`)
56+ if !package. publish . as_ref ( ) . filter ( |publish| publish. is_empty ( ) ) . is_none ( ) {
57+ can_check_crate = false ;
58+ break ;
59+ }
60+ }
61+ } ,
62+ Err ( _) => can_check_crate = false ,
63+ }
64+ }
65+
4566 Self {
4667 cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( |dir| PathBuf :: from ( dir) ) ,
4768 warned_spans : FxHashSet :: default ( ) ,
69+ can_check_crate,
4870 }
4971 }
5072
@@ -135,6 +157,9 @@ impl IncludeFileOutsideProject {
135157
136158impl LateLintPass < ' _ > for IncludeFileOutsideProject {
137159 fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
160+ if !self . can_check_crate {
161+ return ;
162+ }
138163 if !expr. span . from_expansion ( ) {
139164 self . check_hir_id ( cx, expr. span , expr. hir_id ) ;
140165 } else if let ExprKind :: Lit ( lit) = & expr. kind
@@ -150,12 +175,15 @@ impl LateLintPass<'_> for IncludeFileOutsideProject {
150175 fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & ' _ Item < ' _ > ) {
151176 // Interestingly enough, `include!` content is not considered expanded. Which allows us
152177 // to easily filter out items we're not interested into.
153- if !item. span . from_expansion ( ) {
178+ if self . can_check_crate && !item. span . from_expansion ( ) {
154179 self . check_hir_id ( cx, item. span , item. hir_id ( ) ) ;
155180 }
156181 }
157182
158183 fn check_attributes ( & mut self , cx : & LateContext < ' _ > , attrs : & [ Attribute ] ) {
184+ if !self . can_check_crate {
185+ return ;
186+ }
159187 for attr in attrs {
160188 if let Some ( attr) = attr. meta ( ) {
161189 self . check_attribute ( cx, & attr) ;
0 commit comments