1
- use crate :: Lint ;
1
+ use crate :: { Lint , LintExtractor } ;
2
2
use std:: collections:: { BTreeMap , BTreeSet } ;
3
3
use std:: error:: Error ;
4
4
use std:: fmt:: Write ;
5
5
use std:: fs;
6
- use std:: path:: Path ;
7
6
use std:: process:: Command ;
8
7
9
8
static GROUP_DESCRIPTIONS : & [ ( & str , & str ) ] = & [
@@ -15,100 +14,113 @@ static GROUP_DESCRIPTIONS: &[(&str, &str)] = &[
15
14
( "rust-2018-compatibility" , "Lints used to transition code from the 2015 edition to 2018" ) ,
16
15
] ;
17
16
18
- /// Updates the documentation of lint groups.
19
- pub ( crate ) fn generate_group_docs (
20
- lints : & [ Lint ] ,
21
- rustc : crate :: Rustc < ' _ > ,
22
- out_path : & Path ,
23
- ) -> Result < ( ) , Box < dyn Error > > {
24
- let groups = collect_groups ( rustc) ?;
25
- let groups_path = out_path. join ( "groups.md" ) ;
26
- let contents = fs:: read_to_string ( & groups_path)
27
- . map_err ( |e| format ! ( "could not read {}: {}" , groups_path. display( ) , e) ) ?;
28
- let new_contents = contents. replace ( "{{groups-table}}" , & make_groups_table ( lints, & groups) ?) ;
29
- // Delete the output because rustbuild uses hard links in its copies.
30
- let _ = fs:: remove_file ( & groups_path) ;
31
- fs:: write ( & groups_path, new_contents)
32
- . map_err ( |e| format ! ( "could not write to {}: {}" , groups_path. display( ) , e) ) ?;
33
- Ok ( ( ) )
34
- }
35
-
36
17
type LintGroups = BTreeMap < String , BTreeSet < String > > ;
37
18
38
- /// Collects the group names from rustc.
39
- fn collect_groups ( rustc : crate :: Rustc < ' _ > ) -> Result < LintGroups , Box < dyn Error > > {
40
- let mut result = BTreeMap :: new ( ) ;
41
- let mut cmd = Command :: new ( rustc . path ) ;
42
- cmd . arg ( "-Whelp ") ;
43
- let output = cmd . output ( ) . map_err ( |e| format ! ( "failed to run command {:?} \n {}" , cmd , e ) ) ? ;
44
- if !output . status . success ( ) {
45
- return Err ( format ! (
46
- "failed to collect lint info: {:?} \n --- stderr \n {}--- stdout \n {} \n " ,
47
- output. status ,
48
- std :: str :: from_utf8 ( & output . stderr ) . unwrap ( ) ,
49
- std :: str :: from_utf8 ( & output . stdout ) . unwrap ( ) ,
50
- )
51
- . into ( ) ) ;
19
+ impl < ' a > LintExtractor < ' a > {
20
+ /// Updates the documentation of lint groups.
21
+ pub ( crate ) fn generate_group_docs ( & self , lints : & [ Lint ] ) -> Result < ( ) , Box < dyn Error > > {
22
+ let groups = self . collect_groups ( ) ? ;
23
+ let groups_path = self . out_path . join ( "groups.md ") ;
24
+ let contents = fs :: read_to_string ( & groups_path )
25
+ . map_err ( |e| format ! ( "could not read {}: {}" , groups_path . display ( ) , e ) ) ? ;
26
+ let new_contents =
27
+ contents . replace ( "{{groups-table}}" , & self . make_groups_table ( lints , & groups ) ? ) ;
28
+ // Delete the output because rustbuild uses hard links in its copies.
29
+ let _ = fs :: remove_file ( & groups_path ) ;
30
+ fs :: write ( & groups_path , new_contents )
31
+ . map_err ( |e| format ! ( "could not write to {}: {}" , groups_path . display ( ) , e ) ) ? ;
32
+ Ok ( ( ) )
52
33
}
53
- let stdout = std:: str:: from_utf8 ( & output. stdout ) . unwrap ( ) ;
54
- let lines = stdout. lines ( ) ;
55
- let group_start = lines. skip_while ( |line| !line. contains ( "groups provided" ) ) . skip ( 1 ) ;
56
- let table_start = group_start. skip_while ( |line| !line. contains ( "----" ) ) . skip ( 1 ) ;
57
- for line in table_start {
58
- if line. is_empty ( ) {
59
- break ;
34
+
35
+ /// Collects the group names from rustc.
36
+ fn collect_groups ( & self ) -> Result < LintGroups , Box < dyn Error > > {
37
+ let mut result = BTreeMap :: new ( ) ;
38
+ let mut cmd = Command :: new ( self . rustc_path ) ;
39
+ cmd. arg ( "-Whelp" ) ;
40
+ let output = cmd. output ( ) . map_err ( |e| format ! ( "failed to run command {:?}\n {}" , cmd, e) ) ?;
41
+ if !output. status . success ( ) {
42
+ return Err ( format ! (
43
+ "failed to collect lint info: {:?}\n --- stderr\n {}--- stdout\n {}\n " ,
44
+ output. status,
45
+ std:: str :: from_utf8( & output. stderr) . unwrap( ) ,
46
+ std:: str :: from_utf8( & output. stdout) . unwrap( ) ,
47
+ )
48
+ . into ( ) ) ;
60
49
}
61
- let mut parts = line. trim ( ) . splitn ( 2 , ' ' ) ;
62
- let name = parts. next ( ) . expect ( "name in group" ) ;
63
- if name == "warnings" {
64
- // This is special.
65
- continue ;
50
+ let stdout = std:: str:: from_utf8 ( & output. stdout ) . unwrap ( ) ;
51
+ let lines = stdout. lines ( ) ;
52
+ let group_start = lines. skip_while ( |line| !line. contains ( "groups provided" ) ) . skip ( 1 ) ;
53
+ let table_start = group_start. skip_while ( |line| !line. contains ( "----" ) ) . skip ( 1 ) ;
54
+ for line in table_start {
55
+ if line. is_empty ( ) {
56
+ break ;
57
+ }
58
+ let mut parts = line. trim ( ) . splitn ( 2 , ' ' ) ;
59
+ let name = parts. next ( ) . expect ( "name in group" ) ;
60
+ if name == "warnings" {
61
+ // This is special.
62
+ continue ;
63
+ }
64
+ let lints = parts
65
+ . next ( )
66
+ . ok_or_else ( || format ! ( "expected lints following name, got `{}`" , line) ) ?;
67
+ let lints = lints. split ( ',' ) . map ( |l| l. trim ( ) . to_string ( ) ) . collect ( ) ;
68
+ assert ! ( result. insert( name. to_string( ) , lints) . is_none( ) ) ;
66
69
}
67
- let lints =
68
- parts. next ( ) . ok_or_else ( || format ! ( "expected lints following name, got `{}`" , line) ) ?;
69
- let lints = lints. split ( ',' ) . map ( |l| l. trim ( ) . to_string ( ) ) . collect ( ) ;
70
- assert ! ( result. insert( name. to_string( ) , lints) . is_none( ) ) ;
71
- }
72
- if result. is_empty ( ) {
73
- return Err (
74
- format ! ( "expected at least one group in -Whelp output, got:\n {}" , stdout) . into ( )
75
- ) ;
70
+ if result. is_empty ( ) {
71
+ return Err (
72
+ format ! ( "expected at least one group in -Whelp output, got:\n {}" , stdout) . into ( )
73
+ ) ;
74
+ }
75
+ Ok ( result)
76
76
}
77
- Ok ( result)
78
- }
79
77
80
- fn make_groups_table ( lints : & [ Lint ] , groups : & LintGroups ) -> Result < String , Box < dyn Error > > {
81
- let mut result = String :: new ( ) ;
82
- let mut to_link = Vec :: new ( ) ;
83
- result. push_str ( "| Group | Description | Lints |\n " ) ;
84
- result. push_str ( "|-------|-------------|-------|\n " ) ;
85
- result. push_str ( "| warnings | All lints that are set to issue warnings | See [warn-by-default] for the default set of warnings |\n " ) ;
86
- for ( group_name, group_lints) in groups {
87
- let description = GROUP_DESCRIPTIONS . iter ( ) . find ( |( n, _) | n == group_name)
88
- . ok_or_else ( || format ! ( "lint group `{}` does not have a description, please update the GROUP_DESCRIPTIONS list" , group_name) ) ?
89
- . 1 ;
90
- to_link. extend ( group_lints) ;
91
- let brackets: Vec < _ > = group_lints. iter ( ) . map ( |l| format ! ( "[{}]" , l) ) . collect ( ) ;
92
- write ! ( result, "| {} | {} | {} |\n " , group_name, description, brackets. join( ", " ) ) . unwrap ( ) ;
93
- }
94
- result. push ( '\n' ) ;
95
- result. push_str ( "[warn-by-default]: listing/warn-by-default.md\n " ) ;
96
- for lint_name in to_link {
97
- let lint_def =
98
- lints. iter ( ) . find ( |l| l. name == lint_name. replace ( "-" , "_" ) ) . ok_or_else ( || {
99
- format ! (
100
- "`rustc -W help` defined lint `{}` but that lint does not appear to exist" ,
101
- lint_name
102
- )
103
- } ) ?;
104
- write ! (
105
- result,
106
- "[{}]: listing/{}#{}\n " ,
107
- lint_name,
108
- lint_def. level. doc_filename( ) ,
109
- lint_name
110
- )
111
- . unwrap ( ) ;
78
+ fn make_groups_table (
79
+ & self ,
80
+ lints : & [ Lint ] ,
81
+ groups : & LintGroups ,
82
+ ) -> Result < String , Box < dyn Error > > {
83
+ let mut result = String :: new ( ) ;
84
+ let mut to_link = Vec :: new ( ) ;
85
+ result. push_str ( "| Group | Description | Lints |\n " ) ;
86
+ result. push_str ( "|-------|-------------|-------|\n " ) ;
87
+ result. push_str ( "| warnings | All lints that are set to issue warnings | See [warn-by-default] for the default set of warnings |\n " ) ;
88
+ for ( group_name, group_lints) in groups {
89
+ let description = GROUP_DESCRIPTIONS
90
+ . iter ( )
91
+ . find ( |( n, _) | n == group_name)
92
+ . ok_or_else ( || {
93
+ format ! (
94
+ "lint group `{}` does not have a description, \
95
+ please update the GROUP_DESCRIPTIONS list",
96
+ group_name
97
+ )
98
+ } ) ?
99
+ . 1 ;
100
+ to_link. extend ( group_lints) ;
101
+ let brackets: Vec < _ > = group_lints. iter ( ) . map ( |l| format ! ( "[{}]" , l) ) . collect ( ) ;
102
+ write ! ( result, "| {} | {} | {} |\n " , group_name, description, brackets. join( ", " ) )
103
+ . unwrap ( ) ;
104
+ }
105
+ result. push ( '\n' ) ;
106
+ result. push_str ( "[warn-by-default]: listing/warn-by-default.md\n " ) ;
107
+ for lint_name in to_link {
108
+ let lint_def =
109
+ lints. iter ( ) . find ( |l| l. name == lint_name. replace ( "-" , "_" ) ) . ok_or_else ( || {
110
+ format ! (
111
+ "`rustc -W help` defined lint `{}` but that lint does not appear to exist" ,
112
+ lint_name
113
+ )
114
+ } ) ?;
115
+ write ! (
116
+ result,
117
+ "[{}]: listing/{}#{}\n " ,
118
+ lint_name,
119
+ lint_def. level. doc_filename( ) ,
120
+ lint_name
121
+ )
122
+ . unwrap ( ) ;
123
+ }
124
+ Ok ( result)
112
125
}
113
- Ok ( result)
114
126
}
0 commit comments