Skip to content

Commit 382413a

Browse files
committed
fix: do not allow * to match across path segments
1 parent 19c6fee commit 382413a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lintrunner"
3-
version = "0.6.1"
3+
version = "0.6.2"
44
authors = ["Michael Suo <suo@fb.com>"]
55
edition = "2021"
66
description = "A lint running tool and framework."

src/linter.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
path::{path_relative_from, AbsPath},
99
};
1010
use anyhow::{anyhow, bail, ensure, Context, Result};
11-
use glob::Pattern;
11+
use glob::{MatchOptions, Pattern};
1212
use log::{debug, info};
1313

1414
pub struct Linter {
@@ -23,7 +23,17 @@ pub struct Linter {
2323
fn matches_relative_path(base: &Path, from: &Path, pattern: &Pattern) -> bool {
2424
// Unwrap ok because we already checked that both paths are absolute.
2525
let relative_path = path_relative_from(from, base).unwrap();
26-
pattern.matches(relative_path.to_str().unwrap())
26+
pattern.matches_with(
27+
relative_path.to_str().unwrap(),
28+
MatchOptions {
29+
case_sensitive: true,
30+
// Explicitly set this option to true. Most unix implementations do
31+
// not allow `*` to match across path segments, so the default
32+
// (false) behavior is unexpected for people.
33+
require_literal_separator: true,
34+
require_literal_leading_dot: false,
35+
},
36+
)
2737
}
2838

2939
impl Linter {
@@ -183,3 +193,21 @@ impl Linter {
183193
}
184194
}
185195
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use std::path::PathBuf;
200+
201+
use super::*;
202+
203+
// Check that `*` does not match across path segments.
204+
#[test]
205+
fn test_glob_with_separator() -> Result<()> {
206+
assert!(!matches_relative_path(
207+
&PathBuf::from(""),
208+
&PathBuf::from("foo/bar/baz"),
209+
&Pattern::new("foo/b*")?,
210+
));
211+
Ok(())
212+
}
213+
}

0 commit comments

Comments
 (0)