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.
This commit is contained in:
Alexey Shmalko 2019-04-29 20:01:46 +03:00
parent 758dc9af50
commit 8b82f685a5
4 changed files with 18 additions and 4 deletions

View File

@ -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)",

View File

@ -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"

View File

@ -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 {

View File

@ -5,6 +5,7 @@
#![deny(rust_2018_idioms)]
extern crate regex;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;