Skip to content

Commit 0a0187a

Browse files
Do not run include_file_outside_project lint if crate is publish = false
1 parent 72ba639 commit 0a0187a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

clippy_lints/src/include_file_outside_project.rs

Lines changed: 33 additions & 5 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_some() {
57+
can_check_crate = false;
58+
break;
59+
}
60+
}
61+
},
62+
Err(_) => can_check_crate = false,
63+
}
64+
}
65+
4566
Self {
46-
cargo_manifest_dir: std::env::var("CARGO_MANIFEST_DIR").ok().map(|dir| PathBuf::from(dir)),
67+
cargo_manifest_dir: std::env::var("CARGO_MANIFEST_DIR").ok().map(PathBuf::from),
4768
warned_spans: FxHashSet::default(),
69+
can_check_crate,
4870
}
4971
}
5072

@@ -66,12 +88,12 @@ impl IncludeFileOutsideProject {
6688
}
6789
}
6890

69-
fn is_part_of_project_dir(&self, file_path: &PathBuf) -> bool {
91+
fn is_part_of_project_dir(&self, file_path: &Path) -> bool {
7092
if let Some(ref cargo_manifest_dir) = self.cargo_manifest_dir {
7193
// Check if both paths start with the same thing.
7294
let mut file_iter = file_path.iter();
7395

74-
for cargo_item in cargo_manifest_dir.iter() {
96+
for cargo_item in cargo_manifest_dir {
7597
match file_iter.next() {
7698
Some(file_path) if file_path == cargo_item => {},
7799
_ => {
@@ -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
@@ -951,7 +951,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
951951
store.register_late_pass(move |_| Box::new(unused_trait_names::UnusedTraitNames::new(conf)));
952952
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
953953
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
954-
store.register_late_pass(|_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new()));
954+
store.register_late_pass(move |_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new(conf)));
955955
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
956956
// add lints here, do not remove this comment, it's used in `new_lint`
957957
}

0 commit comments

Comments
 (0)