Skip to content

Commit 8b82f68

Browse files
committed
Make find_attr_val a little bit more precise
`find_attr_val(&line, "since")` returns `Some(", issue = ")` when `line` is set to the following line: ``` [unstable(feature = "checked_duration_since", issue = "58402")] ``` Make `find_attr_val` use regex that is a little bit more precise (requires `=` after key name). It still does not handle all cases (e.g., extra leading chars in key name, or escaped quotes in value), but is good enough for now.
1 parent 758dc9a commit 8b82f68

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,7 @@ dependencies = [
35653565
name = "tidy"
35663566
version = "0.1.0"
35673567
dependencies = [
3568+
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
35683569
"serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
35693570
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
35703571
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",

src/tools/tidy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
authors = ["Alex Crichton <alex@alexcrichton.com>"]
55

66
[dependencies]
7+
regex = "1"
78
serde = "1.0.8"
89
serde_derive = "1.0.8"
910
serde_json = "1.0.2"

src/tools/tidy/src/features.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::fs::{self, File};
1414
use std::io::prelude::*;
1515
use std::path::Path;
1616

17+
use regex::{Regex, escape};
18+
1719
#[derive(Debug, PartialEq, Clone)]
1820
pub enum Status {
1921
Stable,
@@ -151,10 +153,19 @@ pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
151153
}
152154

153155
fn find_attr_val<'a>(line: &'a str, attr: &str) -> Option<&'a str> {
154-
line.find(attr)
155-
.and_then(|i| line[i..].find('"').map(|j| i + j + 1))
156-
.and_then(|i| line[i..].find('"').map(|j| (i, i + j)))
157-
.map(|(i, j)| &line[i..j])
156+
let r = Regex::new(&format!(r#"{} *= *"([^"]*)""#, escape(attr)))
157+
.expect("malformed regex for find_attr_val");
158+
r.captures(line)
159+
.and_then(|c| c.get(1))
160+
.map(|m| m.as_str())
161+
}
162+
163+
#[test]
164+
fn test_find_attr_val() {
165+
let s = r#"#[unstable(feature = "checked_duration_since", issue = "58402")]"#;
166+
assert_eq!(find_attr_val(s, "feature"), Some("checked_duration_since"));
167+
assert_eq!(find_attr_val(s, "issue"), Some("58402"));
168+
assert_eq!(find_attr_val(s, "since"), None);
158169
}
159170

160171
fn test_filen_gate(filen_underscore: &str, features: &mut Features) -> bool {

src/tools/tidy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
#![deny(rust_2018_idioms)]
77

8+
extern crate regex;
89
extern crate serde_json;
910
#[macro_use]
1011
extern crate serde_derive;

0 commit comments

Comments
 (0)