1
- use crate :: clippy_project_root;
1
+ use crate :: utils :: { UpdateMode , clippy_project_root, exit_with_failure , replace_region_in_file } ;
2
2
use aho_corasick:: AhoCorasickBuilder ;
3
3
use itertools:: Itertools ;
4
4
use rustc_lexer:: { LiteralKind , TokenKind , tokenize, unescape} ;
@@ -17,12 +17,6 @@ const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev u
17
17
18
18
const DOCS_LINK : & str = "https://rust-lang.github.io/rust-clippy/master/index.html" ;
19
19
20
- #[ derive( Clone , Copy , PartialEq , Eq ) ]
21
- pub enum UpdateMode {
22
- Check ,
23
- Change ,
24
- }
25
-
26
20
/// Runs the `update_lints` command.
27
21
///
28
22
/// This updates various generated values from the lint source code.
@@ -511,14 +505,6 @@ fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str)
511
505
}
512
506
}
513
507
514
- fn exit_with_failure ( ) {
515
- println ! (
516
- "Not all lints defined properly. \
517
- Please run `cargo dev update_lints` to make sure all lints are defined properly."
518
- ) ;
519
- std:: process:: exit ( 1 ) ;
520
- }
521
-
522
508
/// Lint data parsed from the Clippy source code.
523
509
#[ derive( Clone , PartialEq , Eq , Debug ) ]
524
510
struct Lint {
@@ -851,61 +837,6 @@ fn remove_line_splices(s: &str) -> String {
851
837
} ) ;
852
838
res
853
839
}
854
-
855
- /// Replaces a region in a file delimited by two lines matching regexes.
856
- ///
857
- /// `path` is the relative path to the file on which you want to perform the replacement.
858
- ///
859
- /// See `replace_region_in_text` for documentation of the other options.
860
- ///
861
- /// # Panics
862
- ///
863
- /// Panics if the path could not read or then written
864
- fn replace_region_in_file (
865
- update_mode : UpdateMode ,
866
- path : & Path ,
867
- start : & str ,
868
- end : & str ,
869
- write_replacement : impl FnMut ( & mut String ) ,
870
- ) {
871
- let contents = fs:: read_to_string ( path) . unwrap_or_else ( |e| panic ! ( "Cannot read from `{}`: {e}" , path. display( ) ) ) ;
872
- let new_contents = match replace_region_in_text ( & contents, start, end, write_replacement) {
873
- Ok ( x) => x,
874
- Err ( delim) => panic ! ( "Couldn't find `{delim}` in file `{}`" , path. display( ) ) ,
875
- } ;
876
-
877
- match update_mode {
878
- UpdateMode :: Check if contents != new_contents => exit_with_failure ( ) ,
879
- UpdateMode :: Check => ( ) ,
880
- UpdateMode :: Change => {
881
- if let Err ( e) = fs:: write ( path, new_contents. as_bytes ( ) ) {
882
- panic ! ( "Cannot write to `{}`: {e}" , path. display( ) ) ;
883
- }
884
- } ,
885
- }
886
- }
887
-
888
- /// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
889
- /// were found, or the missing delimiter if not.
890
- fn replace_region_in_text < ' a > (
891
- text : & str ,
892
- start : & ' a str ,
893
- end : & ' a str ,
894
- mut write_replacement : impl FnMut ( & mut String ) ,
895
- ) -> Result < String , & ' a str > {
896
- let ( text_start, rest) = text. split_once ( start) . ok_or ( start) ?;
897
- let ( _, text_end) = rest. split_once ( end) . ok_or ( end) ?;
898
-
899
- let mut res = String :: with_capacity ( text. len ( ) + 4096 ) ;
900
- res. push_str ( text_start) ;
901
- res. push_str ( start) ;
902
- write_replacement ( & mut res) ;
903
- res. push_str ( end) ;
904
- res. push_str ( text_end) ;
905
-
906
- Ok ( res)
907
- }
908
-
909
840
fn try_rename_file ( old_name : & Path , new_name : & Path ) -> bool {
910
841
match OpenOptions :: new ( ) . create_new ( true ) . write ( true ) . open ( new_name) {
911
842
Ok ( file) => drop ( file) ,
0 commit comments