Skip to content

Commit 12c3acd

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

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

src/ignore_path.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ 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+
if p.is_absolute() && !p.starts_with(self.ignore_set.path()) {
27+
false
28+
} else {
29+
self.ignore_set
30+
.matched_path_or_any_parents(p, false)
31+
.is_ignore()
32+
}
33+
}
2934
}
3035
}
3136
}
@@ -70,4 +75,21 @@ mod test {
7075
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/a.rs"))));
7176
assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/what.rs"))));
7277
}
78+
79+
#[nightly_only_test]
80+
#[test]
81+
fn test_ignore_path_outside_root() {
82+
//See: https://github.com/rust-lang/rustfmt/issues/6843
83+
use crate::config::{Config, FileName};
84+
use crate::ignore_path::IgnorePathSet;
85+
86+
let config_path = std::env::temp_dir().join("a").join("rustfmt.toml");
87+
let file_path = std::env::temp_dir().join("b").join("foo.rs");
88+
89+
let config = Config::from_toml(r#"ignore = ["bar.rs"]"#, &config_path).unwrap();
90+
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();
91+
92+
//should not panic
93+
assert!(!ignore_path_set.is_match(&FileName::Real(file_path)));
94+
}
7395
}

0 commit comments

Comments
 (0)