@@ -24,8 +24,16 @@ declare_lint! {
2424 "checks for the presence of `_`, `::` or camel-case outside ticks in documentation"
2525}
2626
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+ }
2937
3038impl LintPass for Doc {
3139 fn get_lints ( & self ) -> LintArray {
@@ -35,11 +43,11 @@ impl LintPass for Doc {
3543
3644impl EarlyLintPass for Doc {
3745 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 ) ;
3947 }
4048
4149 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 ) ;
4351 }
4452}
4553
@@ -73,7 +81,7 @@ fn collect_doc(attrs: &[ast::Attribute]) -> (Cow<str>, Option<Span>) {
7381 }
7482}
7583
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 ) {
7785 let ( doc, span) = collect_doc ( attrs) ;
7886 let span = span. unwrap_or ( default_span) ;
7987
@@ -100,15 +108,19 @@ pub fn check_attrs<'a>(cx: &EarlyContext, attrs: &'a [ast::Attribute], default_s
100108 }
101109
102110 if !in_ticks {
103- check_word ( cx, word, span) ;
111+ check_word ( cx, valid_idents , word, span) ;
104112 }
105113 }
106114}
107115
108- fn check_word ( cx : & EarlyContext , word : & str , span : Span ) {
116+ fn check_word ( cx : & EarlyContext , valid_idents : & [ String ] , word : & str , span : Span ) {
109117 /// Checks if a string a camel-case, ie. contains at least two uppercase letter (`Clippy` is
110118 /// ok) and one lower-case letter (`NASA` is ok). Plural are also excluded (`IDs` is ok).
111119 fn is_camel_case ( s : & str ) -> bool {
120+ if s. starts_with ( |c : char | c. is_digit ( 10 ) ) {
121+ return false ;
122+ }
123+
112124 let s = if s. ends_with ( 's' ) {
113125 & s[ ..s. len ( ) -1 ]
114126 } else {
@@ -134,6 +146,10 @@ fn check_word(cx: &EarlyContext, word: &str, span: Span) {
134146 // Or even as in `_foo bar_` which is emphasized.
135147 let word = word. trim_matches ( |c : char | !c. is_alphanumeric ( ) ) ;
136148
149+ if valid_idents. iter ( ) . any ( |i| i == word) {
150+ return ;
151+ }
152+
137153 if has_underscore ( word) || word. contains ( "::" ) || is_camel_case ( word) {
138154 span_lint ( cx, DOC_MARKDOWN , span, & format ! ( "you should put `{}` between ticks in the documentation" , word) ) ;
139155 }
0 commit comments