Skip to content

Commit

Permalink
Do not run include_file_outside_project lint if crate is `publish =…
Browse files Browse the repository at this point in the history
… false`
  • Loading branch information
GuillaumeGomez committed Nov 4, 2024
1 parent 982fbe1 commit 61eb990
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
32 changes: 30 additions & 2 deletions clippy_lints/src/include_file_outside_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::{FileName, Span, sym};

use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::root_macro_call_first_node;

use cargo_metadata::MetadataCommand;

use std::path::{Path, PathBuf};

declare_clippy_lint! {
Expand Down Expand Up @@ -36,15 +39,34 @@ declare_clippy_lint! {
pub(crate) struct IncludeFileOutsideProject {
cargo_manifest_dir: Option<PathBuf>,
warned_spans: FxHashSet<PathBuf>,
can_check_crate: bool,
}

impl_lint_pass!(IncludeFileOutsideProject => [INCLUDE_FILE_OUTSIDE_PROJECT]);

impl IncludeFileOutsideProject {
pub(crate) fn new() -> Self {
pub(crate) fn new(conf: &'static Conf) -> Self {
let mut can_check_crate = true;
if !conf.cargo_ignore_publish {
match MetadataCommand::new().no_deps().exec() {
Ok(metadata) => {
for package in &metadata.packages {
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
// or if the vector isn't empty (`publish = ["something"]`)
if !package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() {
can_check_crate = false;
break;
}
}
},
Err(_) => can_check_crate = false,
}
}

Self {
cargo_manifest_dir: std::env::var("CARGO_MANIFEST_DIR").ok().map(|dir| PathBuf::from(dir)),
warned_spans: FxHashSet::default(),
can_check_crate,
}
}

Expand Down Expand Up @@ -135,6 +157,9 @@ impl IncludeFileOutsideProject {

impl LateLintPass<'_> for IncludeFileOutsideProject {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
if !self.can_check_crate {
return;
}
if !expr.span.from_expansion() {
self.check_hir_id(cx, expr.span, expr.hir_id);
} else if let ExprKind::Lit(lit) = &expr.kind
Expand All @@ -150,12 +175,15 @@ impl LateLintPass<'_> for IncludeFileOutsideProject {
fn check_item(&mut self, cx: &LateContext<'_>, item: &'_ Item<'_>) {
// Interestingly enough, `include!` content is not considered expanded. Which allows us
// to easily filter out items we're not interested into.
if !item.span.from_expansion() {
if self.can_check_crate && !item.span.from_expansion() {
self.check_hir_id(cx, item.span, item.hir_id());
}
}

fn check_attributes(&mut self, cx: &LateContext<'_>, attrs: &[Attribute]) {
if !self.can_check_crate {
return;
}
for attr in attrs {
if let Some(attr) = attr.meta() {
self.check_attribute(cx, &attr);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(move |_| Box::new(unused_trait_names::UnusedTraitNames::new(conf)));
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
store.register_late_pass(|_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new()));
store.register_late_pass(move |_| Box::new(include_file_outside_project::IncludeFileOutsideProject::new(conf)));
// add lints here, do not remove this comment, it's used in `new_lint`
}

0 comments on commit 61eb990

Please sign in to comment.