]> git.lizzy.rs Git - rust.git/blobdiff - src/config/config_type.rs
Fix bugs related to file-lines (#3684)
[rust.git] / src / config / config_type.rs
index 82a19c5898f1a9be474d2a5c9b1641ad29c11808..7b91f54877d3820cce6e1c058622b19afc8a9175 100644 (file)
@@ -2,7 +2,7 @@
 use crate::config::options::{IgnoreList, WidthHeuristics};
 
 /// Trait for types that can be used in `Config`.
-pub trait ConfigType: Sized {
+pub(crate) trait ConfigType: Sized {
     /// Returns hint text for use in `Config::print_docs()`. For enum types, this is a
     /// pipe-separated list of variants; for other types it returns "<type>".
     fn doc_hint() -> String;
@@ -50,22 +50,6 @@ fn doc_hint() -> String {
     }
 }
 
-/// Checks if we're in a nightly build.
-///
-/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap
-/// to "stable", "beta", or "nightly" depending on what toolchain is being built.
-/// If we are being built as part of the stable or beta toolchains, we want
-/// to disable unstable configuration options.
-///
-/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`),
-/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the
-/// nightly compiler when installed from crates.io, default to nightly mode.
-macro_rules! is_nightly_channel {
-    () => {
-        option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev")
-    };
-}
-
 macro_rules! create_config {
     ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
         #[cfg(test)]
@@ -75,6 +59,7 @@ macro_rules! create_config {
         use serde::{Deserialize, Serialize};
 
         #[derive(Clone)]
+        #[allow(unreachable_pub)]
         pub struct Config {
             // if a license_template_path has been specified, successfully read, parsed and compiled
             // into a regex, it will be stored here
@@ -91,6 +76,7 @@ pub struct Config {
         // We first parse into `PartialConfig`, then create a default `Config`
         // and overwrite the properties with corresponding values from `PartialConfig`.
         #[derive(Deserialize, Serialize, Clone)]
+        #[allow(unreachable_pub)]
         pub struct PartialConfig {
             $(pub $i: Option<$ty>),+
         }
@@ -100,10 +86,12 @@ pub struct PartialConfig {
         // `config.set().option(false)`. It's pretty ugly. Consider replacing
         // with `config.set_option(false)` if we ever get a stable/usable
         // `concat_idents!()`.
+        #[allow(unreachable_pub)]
         pub struct ConfigSetter<'a>(&'a mut Config);
 
         impl<'a> ConfigSetter<'a> {
             $(
+            #[allow(unreachable_pub)]
             pub fn $i(&mut self, value: $ty) {
                 (self.0).$i.2 = value;
                 match stringify!($i) {
@@ -117,10 +105,12 @@ pub fn $i(&mut self, value: $ty) {
 
         // Query each option, returns true if the user set the option, false if
         // a default was used.
+        #[allow(unreachable_pub)]
         pub struct ConfigWasSet<'a>(&'a Config);
 
         impl<'a> ConfigWasSet<'a> {
             $(
+            #[allow(unreachable_pub)]
             pub fn $i(&self) -> bool {
                 (self.0).$i.1
             }
@@ -129,16 +119,19 @@ pub fn $i(&self) -> bool {
 
         impl Config {
             $(
+            #[allow(unreachable_pub)]
             pub fn $i(&self) -> $ty {
                 self.$i.0.set(true);
                 self.$i.2.clone()
             }
             )+
 
+            #[allow(unreachable_pub)]
             pub fn set(&mut self) -> ConfigSetter<'_> {
                 ConfigSetter(self)
             }
 
+            #[allow(unreachable_pub)]
             pub fn was_set(&self) -> ConfigWasSet<'_> {
                 ConfigWasSet(self)
             }
@@ -150,7 +143,7 @@ fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Confi
                         self.$i.1 = true;
                         self.$i.2 = val;
                     } else {
-                        if is_nightly_channel!() {
+                        if crate::is_nightly_channel!() {
                             self.$i.1 = true;
                             self.$i.2 = val;
                         } else {
@@ -185,6 +178,7 @@ pub(crate) fn is_valid_name(name: &str) -> bool {
                 }
             }
 
+            #[allow(unreachable_pub)]
             pub fn used_options(&self) -> PartialConfig {
                 PartialConfig {
                     $(
@@ -197,6 +191,7 @@ pub fn used_options(&self) -> PartialConfig {
                 }
             }
 
+            #[allow(unreachable_pub)]
             pub fn all_options(&self) -> PartialConfig {
                 PartialConfig {
                     $(
@@ -205,6 +200,7 @@ pub fn all_options(&self) -> PartialConfig {
                 }
             }
 
+            #[allow(unreachable_pub)]
             pub fn override_value(&mut self, key: &str, val: &str)
             {
                 match key {
@@ -228,12 +224,14 @@ pub fn override_value(&mut self, key: &str, val: &str)
                 }
             }
 
+            #[allow(unreachable_pub)]
             pub fn is_hidden_option(name: &str) -> bool {
                 const HIDE_OPTIONS: [&str; 4] =
                     ["verbose", "verbose_diff", "file_lines", "width_heuristics"];
                 HIDE_OPTIONS.contains(&name)
             }
 
+            #[allow(unreachable_pub)]
             pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
                 use std::cmp;
                 let max = 0;
@@ -251,11 +249,15 @@ pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
                             }
                             name_out.push_str(name_raw);
                             name_out.push(' ');
+                            let mut default_str = format!("{}", $def);
+                            if default_str.is_empty() {
+                                default_str = String::from("\"\"");
+                            }
                             writeln!(out,
-                                    "{}{} Default: {:?}{}",
+                                    "{}{} Default: {}{}",
                                     name_out,
                                     <$ty>::doc_hint(),
-                                    $def,
+                                    default_str,
                                     if !$stb { " (unstable)" } else { "" }).unwrap();
                             $(
                                 writeln!(out, "{}{}", space_str, $dstring).unwrap();
@@ -293,6 +295,7 @@ fn set_ignore(&mut self, dir: &Path) {
                 self.ignore.2.add_prefix(dir);
             }
 
+            #[allow(unreachable_pub)]
             /// Returns `true` if the config key was explicitly set and is the default value.
             pub fn is_default(&self, key: &str) -> bool {
                 $(