Skip to content

Commit 03625aa

Browse files
tesujitopecongiro
authored andcommitted
Some minor cleanups (#3983)
1 parent 75ecd60 commit 03625aa

File tree

15 files changed

+56
-76
lines changed

15 files changed

+56
-76
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
sudo: false
21
language: rust
32
rust: nightly
43
os: linux

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ install:
4040
- if "%TARGET%" == "i686-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw32\bin
4141
- if "%TARGET%" == "x86_64-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw64\bin
4242
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
43-
- rustup-init.exe --default-host %TARGET% --default-toolchain %CHANNEL% -y
43+
- rustup-init.exe --default-host %TARGET% --default-toolchain %CHANNEL% --profile minimal -y
4444
- rustc -Vv
4545
- cargo -V
4646

src/bin/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::{format_err, Result};
2-
use env_logger;
32
use io::Error as IoError;
43
use thiserror::Error;
54

@@ -310,10 +309,10 @@ fn format(
310309

311310
for file in files {
312311
if !file.exists() {
313-
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
312+
eprintln!("Error: file `{}` does not exist", file.display());
314313
session.add_operational_error();
315314
} else if file.is_dir() {
316-
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
315+
eprintln!("Error: `{}` is a directory", file.display());
317316
session.add_operational_error();
318317
} else {
319318
// Check the file directory if the config-path could not be read or not provided

src/cargo-fmt/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#![deny(warnings)]
44

5-
use cargo_metadata;
6-
75
use std::cmp::Ordering;
86
use std::collections::{BTreeMap, BTreeSet};
97
use std::env;

src/config/config_type.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl ConfigType for IgnoreList {
5151
}
5252

5353
macro_rules! create_config {
54-
($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
54+
($($i:ident: $Ty:ty, $def:expr, $is_stable:literal, $dstring:literal;)+) => (
5555
#[cfg(test)]
5656
use std::collections::HashSet;
5757
use std::io::Write;
@@ -69,8 +69,8 @@ macro_rules! create_config {
6969
pub license_template: Option<Regex>,
7070
// For each config item, we store a bool indicating whether it has
7171
// been accessed and the value, and a bool whether the option was
72-
// manually initialised, or taken from the default,
73-
$($i: (Cell<bool>, bool, $ty, bool)),+
72+
// manually initialized, or taken from the default,
73+
$($i: (Cell<bool>, bool, $Ty, bool)),+
7474
}
7575

7676
// Just like the Config struct but with each property wrapped
@@ -81,7 +81,7 @@ macro_rules! create_config {
8181
#[derive(Deserialize, Serialize, Clone)]
8282
#[allow(unreachable_pub)]
8383
pub struct PartialConfig {
84-
$(pub $i: Option<$ty>),+
84+
$(pub $i: Option<$Ty>),+
8585
}
8686

8787
// Macro hygiene won't allow us to make `set_$i()` methods on Config
@@ -95,7 +95,7 @@ macro_rules! create_config {
9595
impl<'a> ConfigSetter<'a> {
9696
$(
9797
#[allow(unreachable_pub)]
98-
pub fn $i(&mut self, value: $ty) {
98+
pub fn $i(&mut self, value: $Ty) {
9999
(self.0).$i.2 = value;
100100
match stringify!($i) {
101101
"max_width" | "use_small_heuristics" => self.0.set_heuristics(),
@@ -123,7 +123,7 @@ macro_rules! create_config {
123123
impl Config {
124124
$(
125125
#[allow(unreachable_pub)]
126-
pub fn $i(&self) -> $ty {
126+
pub fn $i(&self) -> $Ty {
127127
self.$i.0.set(true);
128128
self.$i.2.clone()
129129
}
@@ -173,12 +173,11 @@ macro_rules! create_config {
173173

174174
/// Returns a hash set initialized with every user-facing config option name.
175175
#[cfg(test)]
176-
pub(crate) fn hash_set() -> HashSet<String> {
177-
let mut hash_set = HashSet::new();
178-
$(
179-
hash_set.insert(stringify!($i).to_owned());
180-
)+
181-
hash_set
176+
pub(crate) fn hash_set() -> HashSet<&'static str> {
177+
[$(
178+
stringify!($i),
179+
)+]
180+
.iter().copied().collect()
182181
}
183182

184183
pub(crate) fn is_valid_name(name: &str) -> bool {
@@ -194,7 +193,7 @@ macro_rules! create_config {
194193
pub fn is_valid_key_val(key: &str, val: &str) -> bool {
195194
match key {
196195
$(
197-
stringify!($i) => val.parse::<$ty>().is_ok(),
196+
stringify!($i) => val.parse::<$Ty>().is_ok(),
198197
)+
199198
_ => false,
200199
}
@@ -229,11 +228,11 @@ macro_rules! create_config {
229228
$(
230229
stringify!($i) => {
231230
self.$i.1 = true;
232-
self.$i.2 = val.parse::<$ty>()
231+
self.$i.2 = val.parse::<$Ty>()
233232
.expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
234233
stringify!($i),
235234
val,
236-
stringify!($ty)));
235+
stringify!($Ty)));
237236
}
238237
)+
239238
_ => panic!("Unknown config key in override: {}", key)
@@ -258,12 +257,13 @@ macro_rules! create_config {
258257
#[allow(unreachable_pub)]
259258
pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
260259
use std::cmp;
261-
let max = 0;
262-
$( let max = cmp::max(max, stringify!($i).len()+1); )+
260+
let mut max = 0;
261+
$( max = cmp::max(max, stringify!($i).len()); )+
262+
max += 1;
263263
let space_str = " ".repeat(max);
264264
writeln!(out, "Configuration Options:").unwrap();
265265
$(
266-
if $stb || include_unstable {
266+
if $is_stable || include_unstable {
267267
let name_raw = stringify!($i);
268268

269269
if !Config::is_hidden_option(name_raw) {
@@ -277,16 +277,15 @@ macro_rules! create_config {
277277
if default_str.is_empty() {
278278
default_str = String::from("\"\"");
279279
}
280-
writeln!(out,
281-
"{}{} Default: {}{}",
282-
name_out,
283-
<$ty>::doc_hint(),
284-
default_str,
285-
if !$stb { " (unstable)" } else { "" }).unwrap();
286-
$(
287-
writeln!(out, "{}{}", space_str, $dstring).unwrap();
288-
)+
289-
writeln!(out).unwrap();
280+
writeln!(
281+
out,
282+
"{}{} Default: {}{}",
283+
name_out,
284+
<$Ty>::doc_hint(),
285+
default_str,
286+
if !$is_stable { " (unstable)" } else { "" },
287+
).unwrap();
288+
writeln!(out, "{}{}\n", space_str, $dstring).unwrap();
290289
}
291290
}
292291
)+
@@ -339,7 +338,7 @@ macro_rules! create_config {
339338
Self {
340339
license_template: None,
341340
$(
342-
$i: (Cell::new(false), false, $def, $stb),
341+
$i: (Cell::new(false), false, $def, $is_stable),
343342
)+
344343
}
345344
}

src/config/file_lines.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl From<source_map::FileName> for FileName {
3939
impl fmt::Display for FileName {
4040
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4141
match self {
42-
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
42+
FileName::Real(p) => write!(f, "{}", p.display()),
4343
FileName::Stdin => write!(f, "stdin"),
4444
}
4545
}

src/config/license.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fs::File;
33
use std::io;
44
use std::io::Read;
55

6-
use regex;
76
use regex::Regex;
87

98
#[derive(Debug)]

src/config/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,26 +359,26 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
359359
}
360360

361361
fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
362-
let config_path_not_found = |path: &str| -> Result<Option<PathBuf>, Error> {
362+
let config_path_not_found = |path: &Path| -> Result<Option<PathBuf>, Error> {
363363
Err(Error::new(
364364
ErrorKind::NotFound,
365365
format!(
366366
"Error: unable to find a config file for the given path: `{}`",
367-
path
367+
path.display(),
368368
),
369369
))
370370
};
371371

372372
// Read the config_path and convert to parent dir if a file is provided.
373373
// If a config file cannot be found from the given path, return error.
374374
match options.config_path() {
375-
Some(path) if !path.exists() => config_path_not_found(path.to_str().unwrap()),
375+
Some(path) if !path.exists() => config_path_not_found(path),
376376
Some(path) if path.is_dir() => {
377377
let config_file_path = get_toml_path(path)?;
378378
if config_file_path.is_some() {
379379
Ok(config_file_path)
380380
} else {
381-
config_path_not_found(path.to_str().unwrap())
381+
config_path_not_found(path)
382382
}
383383
}
384384
path => Ok(path.map(ToOwned::to_owned)),

src/config/options.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::{hash_set, HashSet};
22
use std::fmt;
33
use std::path::{Path, PathBuf};
44

5-
use itertools::Itertools;
65
use rustfmt_config_proc_macro::config_type;
76
use serde::de::{SeqAccess, Visitor};
87
use serde::ser::SerializeSeq;
@@ -249,7 +248,7 @@ impl WidthHeuristics {
249248
}
250249
}
251250

252-
impl ::std::str::FromStr for WidthHeuristics {
251+
impl std::str::FromStr for WidthHeuristics {
253252
type Err = &'static str;
254253

255254
fn from_str(_: &str) -> Result<Self, Self::Err> {
@@ -274,16 +273,7 @@ pub struct IgnoreList {
274273

275274
impl fmt::Display for IgnoreList {
276275
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
277-
write!(
278-
f,
279-
"[{}]",
280-
self.path_set
281-
.iter()
282-
.format_with(", ", |path, f| f(&format_args!(
283-
"{}",
284-
path.to_string_lossy()
285-
)))
286-
)
276+
f.debug_list().entries(self.path_set.iter()).finish()
287277
}
288278
}
289279

@@ -350,7 +340,7 @@ impl IgnoreList {
350340
}
351341
}
352342

353-
impl ::std::str::FromStr for IgnoreList {
343+
impl std::str::FromStr for IgnoreList {
354344
type Err = &'static str;
355345

356346
fn from_str(_: &str) -> Result<Self, Self::Err> {
@@ -365,7 +355,7 @@ pub trait CliOptions {
365355
fn config_path(&self) -> Option<&Path>;
366356
}
367357

368-
/// The edition of the syntax and semntics of code (RFC 2052).
358+
/// The edition of the syntax and semantics of code (RFC 2052).
369359
#[config_type]
370360
pub enum Edition {
371361
#[value = "2015"]
@@ -379,16 +369,16 @@ pub enum Edition {
379369
}
380370

381371
impl Default for Edition {
382-
fn default() -> Edition {
383-
Edition::Edition2018
372+
fn default() -> Self {
373+
Self::Edition2018
384374
}
385375
}
386376

387-
impl Edition {
388-
pub(crate) fn to_libsyntax_pos_edition(self) -> syntax_pos::edition::Edition {
389-
match self {
390-
Edition::Edition2015 => syntax_pos::edition::Edition::Edition2015,
391-
Edition::Edition2018 => syntax_pos::edition::Edition::Edition2018,
377+
impl From<Edition> for syntax_pos::edition::Edition {
378+
fn from(edition: Edition) -> Self {
379+
match edition {
380+
Edition::Edition2015 => Self::Edition2015,
381+
Edition::Edition2018 => Self::Edition2018,
392382
}
393383
}
394384
}

src/format-diff/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
#![deny(warnings)]
66

7-
use env_logger;
87
#[macro_use]
98
extern crate log;
10-
use regex;
119
use serde::{Deserialize, Serialize};
1210
use serde_json as json;
1311
use thiserror::Error;

0 commit comments

Comments
 (0)