@@ -24,8 +24,16 @@ declare_lint! {
24
24
"checks for the presence of `_`, `::` or camel-case outside ticks in documentation"
25
25
}
26
26
27
- #[ derive( Copy , Clone ) ]
28
- pub struct Doc ;
27
+ #[ derive( Clone ) ]
28
+ pub struct Doc {
29
+ valid_idents : Vec < String > ,
30
+ }
31
+
32
+ impl Doc {
33
+ pub fn new ( valid_idents : Vec < String > ) -> Self {
34
+ Doc { valid_idents : valid_idents }
35
+ }
36
+ }
29
37
30
38
impl LintPass for Doc {
31
39
fn get_lints ( & self ) -> LintArray {
@@ -35,11 +43,11 @@ impl LintPass for Doc {
35
43
36
44
impl EarlyLintPass for Doc {
37
45
fn check_crate ( & mut self , cx : & EarlyContext , krate : & ast:: Crate ) {
38
- check_attrs ( cx, & krate. attrs , krate. span ) ;
46
+ check_attrs ( cx, & self . valid_idents , & krate. attrs , krate. span ) ;
39
47
}
40
48
41
49
fn check_item ( & mut self , cx : & EarlyContext , item : & ast:: Item ) {
42
- check_attrs ( cx, & item. attrs , item. span ) ;
50
+ check_attrs ( cx, & self . valid_idents , & item. attrs , item. span ) ;
43
51
}
44
52
}
45
53
@@ -73,7 +81,7 @@ fn collect_doc(attrs: &[ast::Attribute]) -> (Cow<str>, Option<Span>) {
73
81
}
74
82
}
75
83
76
- pub fn check_attrs < ' a > ( cx : & EarlyContext , attrs : & ' a [ ast:: Attribute ] , default_span : Span ) {
84
+ pub fn check_attrs < ' a > ( cx : & EarlyContext , valid_idents : & [ String ] , attrs : & ' a [ ast:: Attribute ] , default_span : Span ) {
77
85
let ( doc, span) = collect_doc ( attrs) ;
78
86
let span = span. unwrap_or ( default_span) ;
79
87
@@ -100,15 +108,19 @@ pub fn check_attrs<'a>(cx: &EarlyContext, attrs: &'a [ast::Attribute], default_s
100
108
}
101
109
102
110
if !in_ticks {
103
- check_word ( cx, word, span) ;
111
+ check_word ( cx, valid_idents , word, span) ;
104
112
}
105
113
}
106
114
}
107
115
108
- fn check_word ( cx : & EarlyContext , word : & str , span : Span ) {
116
+ fn check_word ( cx : & EarlyContext , valid_idents : & [ String ] , word : & str , span : Span ) {
109
117
/// Checks if a string a camel-case, ie. contains at least two uppercase letter (`Clippy` is
110
118
/// ok) and one lower-case letter (`NASA` is ok). Plural are also excluded (`IDs` is ok).
111
119
fn is_camel_case ( s : & str ) -> bool {
120
+ if s. starts_with ( |c : char | c. is_digit ( 10 ) ) {
121
+ return false ;
122
+ }
123
+
112
124
let s = if s. ends_with ( 's' ) {
113
125
& s[ ..s. len ( ) -1 ]
114
126
} else {
@@ -134,6 +146,10 @@ fn check_word(cx: &EarlyContext, word: &str, span: Span) {
134
146
// Or even as in `_foo bar_` which is emphasized.
135
147
let word = word. trim_matches ( |c : char | !c. is_alphanumeric ( ) ) ;
136
148
149
+ if valid_idents. iter ( ) . any ( |i| i == word) {
150
+ return ;
151
+ }
152
+
137
153
if has_underscore ( word) || word. contains ( "::" ) || is_camel_case ( word) {
138
154
span_lint ( cx, DOC_MARKDOWN , span, & format ! ( "you should put `{}` between ticks in the documentation" , word) ) ;
139
155
}
0 commit comments