Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new include_file_outside_project lint #13638

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not run include_file_outside_project lint if crate is `publish =…
… false`
GuillaumeGomez committed Nov 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a6ab226516f2f0f75f52bf581be34c8062043ebd
38 changes: 33 additions & 5 deletions clippy_lints/src/include_file_outside_project.rs
Original file line number Diff line number Diff line change
@@ -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! {
@@ -42,15 +45,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 !matches!(package.publish.as_deref(), Some([]) | None) {
can_check_crate = false;
break;
}
}
Comment on lines +57 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have to be a cargo lint if it's doing this to deduplicate the cargo metadata invocation and so it doesn't happen by default

But I don't think we need to here, cargo publish already copies the project files into target/package/crate-x.y.z before checking it builds, which would catch most ../../outside-the-project includes

The case that it wouldn't catch is an include of an absolute path, but it seems reasonable to lint these unconditionally

},
Err(_) => can_check_crate = false,
}
}

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

@@ -72,12 +94,12 @@ impl IncludeFileOutsideProject {
}
}

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

for cargo_item in cargo_manifest_dir.iter() {
for cargo_item in cargo_manifest_dir {
match file_iter.next() {
Some(file_path) if file_path == cargo_item => {},
_ => {
@@ -141,6 +163,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
@@ -156,12 +181,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);
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -951,7 +951,7 @@ 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)));
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
// add lints here, do not remove this comment, it's used in `new_lint`
}