Skip to content

Commit 7dd55d4

Browse files
Do not run include_file_outside_project lint if crate is publish = false
1 parent 111e521 commit 7dd55d4

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! {
@@ -42,15 +45,34 @@ declare_clippy_lint! {
4245
pub(crate) struct IncludeFileOutsideProject {
4346
cargo_manifest_dir: Option<PathBuf>,
4447
warned_spans: FxHashSet<PathBuf>,
48+
can_check_crate: bool,
4549
}
4650

4751
impl_lint_pass!(IncludeFileOutsideProject => [INCLUDE_FILE_OUTSIDE_PROJECT]);
4852

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

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

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)