Skip to content

Commit f777afa

Browse files
nowak-tokensmoelius
authored andcommitted
Don't warn about absolute home paths in build scripts
This change allows abs_home_path to skip checking in build scripts, avoiding false positives when building packages. Build scripts are detected by checking if the crate name is "build_script_build". Update list of architectures Refactor build script detection and add test Updated the logic for detecting build scripts in the `check_expr` method to improve clarity. Added a test to verify that the lint does not trigger in build scripts, ensuring the functionality is documented and tested. Enhance documentation for absolute home path lint Added notes in the README and code comments to clarify that the lint does not warn in build scripts (`build.rs`). Updated the test function name to better reflect its purpose and documented the behavior of the build script detection functionality. Refine test for build script detection Added an allowance for Clippy lint in the test function to prevent warnings during compilation. Skip abs_home_path lint in build scripts The abs_home_path lint checks for absolute paths into the user's home directory, which might not exist in production. However, build scripts often need to reference absolute paths, so skip the lint in build.rs files.
1 parent 5aad720 commit f777afa

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

examples/general/abs_home_path/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ The path might not exist when the code is used in production.
1313

1414
The lint does not apply inside macro arguments. So false negatives could result.
1515

16+
### Note
17+
18+
This lint doesn't warn in build scripts (`build.rs`), as they often need to reference absolute paths.
19+
1620
### Example
1721

1822
```rust

examples/general/abs_home_path/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use dylint_internal::{home, paths};
1111
use once_cell::unsync::OnceCell;
1212
use rustc_ast::ast::LitKind;
1313
use rustc_hir::{Closure, Expr, ExprKind, Item, ItemKind, Node, def_id::DefId};
14-
use rustc_lint::{LateContext, LateLintPass};
14+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1515
use rustc_span::Span;
1616
use std::{
1717
collections::HashSet,
@@ -32,6 +32,10 @@ dylint_linting::impl_late_lint! {
3232
///
3333
/// The lint does not apply inside macro arguments. So false negatives could result.
3434
///
35+
/// ### Note
36+
///
37+
/// This lint doesn't warn in build scripts (`build.rs`), as they often need to reference absolute paths.
38+
///
3539
/// ### Example
3640
///
3741
/// ```rust
@@ -67,6 +71,17 @@ impl<'tcx> LateLintPass<'tcx> for AbsHomePath {
6771
}
6872

6973
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr) {
74+
// Skip build scripts
75+
if cx
76+
.sess()
77+
.opts
78+
.crate_name
79+
.as_ref()
80+
.is_some_and(|crate_name| crate_name == "build_script_build")
81+
{
82+
return;
83+
}
84+
7085
if cx
7186
.tcx
7287
.hir()

0 commit comments

Comments
 (0)