about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorAlexey Shmalko <rasen.dubi@gmail.com>2019-04-29 20:01:46 +0300
committerAlexey Shmalko <rasen.dubi@gmail.com>2019-05-02 16:38:23 +0300
commit8b82f685a57d6a1a3567c2ca6e77efedbedefac2 (patch)
tree212ed497e30a03c39aa2962a9219644e917dc606 /src/tools
parent758dc9af504e2fe75813bd362619231ecc727898 (diff)
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.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/tidy/Cargo.toml1
-rw-r--r--src/tools/tidy/src/features.rs19
-rw-r--r--src/tools/tidy/src/lib.rs1
3 files changed, 17 insertions, 4 deletions
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;