Skip to content

Commit 61eb990

Browse files
Do not run include_file_outside_project lint if crate is publish = false
1 parent 982fbe1 commit 61eb990

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

clippy_lints/src/include_file_outside_project.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::impl_lint_pass;
66
use rustc_span::{FileName, Span, sym};
77

8+
use clippy_config::Conf;
89
use clippy_utils::diagnostics::span_lint_and_then;
910
use clippy_utils::macros::root_macro_call_first_node;
1011

12+
use cargo_metadata::MetadataCommand;
13+
1114
use std::path::{Path, PathBuf};
1215

1316
declare_clippy_lint! {
@@ -36,15 +39,34 @@ declare_clippy_lint! {
3639
pub(crate) struct IncludeFileOutsideProject {
3740
cargo_manifest_dir: Option<PathBuf>,
3841
warned_spans: FxHashSet<PathBuf>,
42+
can_check_crate: bool,
3943
}
4044

4145
impl_lint_pass!(IncludeFileOutsideProject => [INCLUDE_FILE_OUTSIDE_PROJECT]);
4246

4347
impl 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

136158
impl 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);

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
950950
store.register_late_pass(move |_| Box::new(unused_trait_names::UnusedTraitNames::new(conf)));
951951
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
952952
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
953-
store.register_late_pass(|_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new()));
953+
store.register_late_pass(move |_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new(conf)));
954954
// add lints here, do not remove this comment, it's used in `new_lint`
955955
}

0 commit comments

Comments
 (0)