Skip to content

Commit 5feba42

Browse files
committed
Fixed ICE when formatting files outside of toml dir
1 parent 01f2ec7 commit 5feba42

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/ignore_path.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ impl IgnorePathSet {
2222
pub(crate) fn is_match(&self, file_name: &FileName) -> bool {
2323
match file_name {
2424
FileName::Stdin => false,
25-
FileName::Real(p) => self
26-
.ignore_set
27-
.matched_path_or_any_parents(p, false)
28-
.is_ignore(),
25+
FileName::Real(p) => {
26+
let ignore_set_root = self.ignore_set.path();
27+
if p.is_absolute() && !p.starts_with(ignore_set_root) {
28+
false
29+
} else {
30+
self.ignore_set
31+
.matched_path_or_any_parents(p, false)
32+
.is_ignore()
33+
}
34+
}
2935
}
3036
}
3137
}
@@ -70,4 +76,21 @@ mod test {
7076
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/a.rs"))));
7177
assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/what.rs"))));
7278
}
79+
80+
#[nightly_only_test]
81+
#[test]
82+
fn test_ignore_path_outside_root() {
83+
//See: https://github.com/rust-lang/rustfmt/issues/6843
84+
use crate::config::{Config, FileName};
85+
use crate::ignore_path::IgnorePathSet;
86+
use std::path::Path;
87+
88+
let config =
89+
Config::from_toml(r#"ignore = ["bar.rs"]"#, Path::new("./rustfmt.toml")).unwrap();
90+
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();
91+
92+
let outside = std::env::temp_dir().join("foo.rs");
93+
//should not panic
94+
assert!(!ignore_path_set.is_match(&FileName::Real(outside)));
95+
}
7396
}

0 commit comments

Comments
 (0)