1
1
#![ feature( rustc_private) ]
2
2
3
- use anyhow:: { format_err , Result } ;
3
+ use anyhow:: { Result , format_err } ;
4
4
5
5
use io:: Error as IoError ;
6
6
use thiserror:: Error ;
@@ -11,15 +11,15 @@ use tracing_subscriber::EnvFilter;
11
11
use std:: collections:: HashMap ;
12
12
use std:: env;
13
13
use std:: fs:: File ;
14
- use std:: io:: { self , stdout , Read , Write } ;
14
+ use std:: io:: { self , Read , Write , stdout } ;
15
15
use std:: path:: { Path , PathBuf } ;
16
16
use std:: str:: FromStr ;
17
17
18
18
use getopts:: { Matches , Options } ;
19
19
20
20
use crate :: rustfmt:: {
21
- load_config , CliOptions , Color , Config , Edition , EmitMode , FileLines , FileName ,
22
- FormatReportFormatterBuilder , Input , Session , StyleEdition , Verbosity ,
21
+ CliOptions , Color , Config , Edition , EmitMode , FileLines , FileName ,
22
+ FormatReportFormatterBuilder , Input , Session , StyleEdition , Verbosity , Version , load_config ,
23
23
} ;
24
24
25
25
const BUG_REPORT_URL : & str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug" ;
@@ -462,16 +462,15 @@ fn print_version() {
462
462
463
463
fn determine_operation ( matches : & Matches ) -> Result < Operation , OperationError > {
464
464
if matches. opt_present ( "h" ) {
465
- let topic = matches. opt_str ( "h" ) ;
466
- if topic. is_none ( ) {
465
+ let Some ( topic) = matches. opt_str ( "h" ) else {
467
466
return Ok ( Operation :: Help ( HelpOp :: None ) ) ;
468
- } else if topic == Some ( "config" . to_owned ( ) ) {
469
- return Ok ( Operation :: Help ( HelpOp :: Config ) ) ;
470
- } else if topic == Some ( "file-lines" . to_owned ( ) ) && is_nightly ( ) {
471
- return Ok ( Operation :: Help ( HelpOp :: FileLines ) ) ;
472
- } else {
473
- return Err ( OperationError :: UnknownHelpTopic ( topic. unwrap ( ) ) ) ;
474
- }
467
+ } ;
468
+
469
+ return match topic. as_str ( ) {
470
+ "config" => Ok ( Operation :: Help ( HelpOp :: Config ) ) ,
471
+ "file-lines" if is_nightly ( ) => Ok ( Operation :: Help ( HelpOp :: FileLines ) ) ,
472
+ _ => Err ( OperationError :: UnknownHelpTopic ( topic) ) ,
473
+ } ;
475
474
}
476
475
let mut free_matches = matches. free . iter ( ) ;
477
476
@@ -734,6 +733,25 @@ impl CliOptions for GetOptsOptions {
734
733
fn config_path ( & self ) -> Option < & Path > {
735
734
self . config_path . as_deref ( )
736
735
}
736
+
737
+ fn edition ( & self ) -> Option < Edition > {
738
+ self . inline_config
739
+ . get ( "edition" )
740
+ . map_or ( self . edition , |e| Edition :: from_str ( e) . ok ( ) )
741
+ }
742
+
743
+ fn style_edition ( & self ) -> Option < StyleEdition > {
744
+ self . inline_config
745
+ . get ( "style_edition" )
746
+ . map_or ( self . style_edition , |se| StyleEdition :: from_str ( se) . ok ( ) )
747
+ }
748
+
749
+ fn version ( & self ) -> Option < Version > {
750
+ self . inline_config
751
+ . get ( "version" )
752
+ . map ( |version| Version :: from_str ( version) . ok ( ) )
753
+ . flatten ( )
754
+ }
737
755
}
738
756
739
757
fn edition_from_edition_str ( edition_str : & str ) -> Result < Edition > {
@@ -802,6 +820,17 @@ mod test {
802
820
options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "Two" . to_owned ( ) ) ] ) ;
803
821
let config = get_config ( None , Some ( options) ) ;
804
822
assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
823
+ assert_eq ! ( config. overflow_delimited_expr( ) , true ) ;
824
+ }
825
+
826
+ #[ nightly_only_test]
827
+ #[ test]
828
+ fn version_config_file_sets_style_edition_override_correctly ( ) {
829
+ let options = GetOptsOptions :: default ( ) ;
830
+ let config_file = Some ( Path :: new ( "tests/config/style-edition/just-version" ) ) ;
831
+ let config = get_config ( config_file, Some ( options) ) ;
832
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
833
+ assert_eq ! ( config. overflow_delimited_expr( ) , true ) ;
805
834
}
806
835
807
836
#[ nightly_only_test]
@@ -846,6 +875,7 @@ mod test {
846
875
] ) ;
847
876
let config = get_config ( None , Some ( options) ) ;
848
877
assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
878
+ assert_eq ! ( config. overflow_delimited_expr( ) , true ) ;
849
879
}
850
880
851
881
#[ nightly_only_test]
@@ -903,4 +933,36 @@ mod test {
903
933
let config = get_config ( config_file, Some ( options) ) ;
904
934
assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
905
935
}
936
+
937
+ #[ nightly_only_test]
938
+ #[ test]
939
+ fn correct_defaults_for_style_edition_loaded ( ) {
940
+ let mut options = GetOptsOptions :: default ( ) ;
941
+ options. style_edition = Some ( StyleEdition :: Edition2024 ) ;
942
+ let config = get_config ( None , Some ( options) ) ;
943
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
944
+ assert_eq ! ( config. overflow_delimited_expr( ) , true ) ;
945
+ }
946
+
947
+ #[ nightly_only_test]
948
+ #[ test]
949
+ fn style_edition_defaults_overridden_from_config ( ) {
950
+ let options = GetOptsOptions :: default ( ) ;
951
+ let config_file = Some ( Path :: new ( "tests/config/style-edition/overrides" ) ) ;
952
+ let config = get_config ( config_file, Some ( options) ) ;
953
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
954
+ assert_eq ! ( config. overflow_delimited_expr( ) , false ) ;
955
+ }
956
+
957
+ #[ nightly_only_test]
958
+ #[ test]
959
+ fn style_edition_defaults_overridden_from_cli ( ) {
960
+ let mut options = GetOptsOptions :: default ( ) ;
961
+ let config_file = Some ( Path :: new ( "tests/config/style-edition/just-style-edition" ) ) ;
962
+ options. inline_config =
963
+ HashMap :: from ( [ ( "overflow_delimited_expr" . to_owned ( ) , "false" . to_owned ( ) ) ] ) ;
964
+ let config = get_config ( config_file, Some ( options) ) ;
965
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
966
+ assert_eq ! ( config. overflow_delimited_expr( ) , false ) ;
967
+ }
906
968
}
0 commit comments