diff options
| author | Alexey Shmalko <rasen.dubi@gmail.com> | 2019-04-29 20:01:46 +0300 |
|---|---|---|
| committer | Alexey Shmalko <rasen.dubi@gmail.com> | 2019-05-02 16:38:23 +0300 |
| commit | 8b82f685a57d6a1a3567c2ca6e77efedbedefac2 (patch) | |
| tree | 212ed497e30a03c39aa2962a9219644e917dc606 | |
| parent | 758dc9af504e2fe75813bd362619231ecc727898 (diff) | |
| download | rust-8b82f685a57d6a1a3567c2ca6e77efedbedefac2.tar.gz rust-8b82f685a57d6a1a3567c2ca6e77efedbedefac2.zip | |
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.
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | src/tools/tidy/Cargo.toml | 1 | ||||
| -rw-r--r-- | src/tools/tidy/src/features.rs | 19 | ||||
| -rw-r--r-- | src/tools/tidy/src/lib.rs | 1 |
4 files changed, 18 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock index fd6a7a5604e..79ef359bdac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3565,6 +3565,7 @@ dependencies = [ name = "tidy" version = "0.1.0" dependencies = [ + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/tools/tidy/Cargo.toml b/src/tools/tidy/Cargo.toml index f7b491823f8..f5db2487618 100644 --- a/src/tools/tidy/Cargo.toml +++ b/src/tools/tidy/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Alex Crichton <alex@alexcrichton.com>"] [dependencies] +regex = "1" serde = "1.0.8" serde_derive = "1.0.8" serde_json = "1.0.2" diff --git a/src/tools/tidy/src/features.rs b/src/tools/tidy/src/features.rs index 8239fd9dce0..f2b17c74711 100644 --- a/src/tools/tidy/src/features.rs +++ b/src/tools/tidy/src/features.rs @@ -14,6 +14,8 @@ use std::fs::{self, File}; use std::io::prelude::*; use std::path::Path; +use regex::{Regex, escape}; + #[derive(Debug, PartialEq, Clone)] pub enum Status { Stable, @@ -151,10 +153,19 @@ pub fn check(path: &Path, bad: &mut bool, quiet: bool) { } fn find_attr_val<'a>(line: &'a str, attr: &str) -> Option<&'a str> { - line.find(attr) - .and_then(|i| line[i..].find('"').map(|j| i + j + 1)) - .and_then(|i| line[i..].find('"').map(|j| (i, i + j))) - .map(|(i, j)| &line[i..j]) + let r = Regex::new(&format!(r#"{} *= *"([^"]*)""#, escape(attr))) + .expect("malformed regex for find_attr_val"); + r.captures(line) + .and_then(|c| c.get(1)) + .map(|m| m.as_str()) +} + +#[test] +fn test_find_attr_val() { + let s = r#"#[unstable(feature = "checked_duration_since", issue = "58402")]"#; + assert_eq!(find_attr_val(s, "feature"), Some("checked_duration_since")); + assert_eq!(find_attr_val(s, "issue"), Some("58402")); + assert_eq!(find_attr_val(s, "since"), None); } fn test_filen_gate(filen_underscore: &str, features: &mut Features) -> bool { diff --git a/src/tools/tidy/src/lib.rs b/src/tools/tidy/src/lib.rs index c4a1246ffdf..30080452edc 100644 --- a/src/tools/tidy/src/lib.rs +++ b/src/tools/tidy/src/lib.rs @@ -5,6 +5,7 @@ #![deny(rust_2018_idioms)] +extern crate regex; extern crate serde_json; #[macro_use] extern crate serde_derive; |
