Skip to content

Commit

Permalink
Merge pull request #1754 from lzutao/clippy
Browse files Browse the repository at this point in the history
Fix most of clippy warnings
  • Loading branch information
kinnison authored Apr 14, 2019
2 parents 78b7689 + 1a92e18 commit cf07f2b
Show file tree
Hide file tree
Showing 27 changed files with 335 additions and 346 deletions.
12 changes: 6 additions & 6 deletions download/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub mod reqwest_be {

if !res.status().is_success() {
let code: u16 = res.status().into();
return Err(ErrorKind::HttpStatus(code as u32).into());
return Err(ErrorKind::HttpStatus(u32::from(code)).into());
}

let buffer_size = 0x10000;
Expand Down Expand Up @@ -345,13 +345,13 @@ pub mod reqwest_be {
return Err(ErrorKind::FileNotFound.into());
}

let ref mut f = fs::File::open(src).chain_err(|| "unable to open downloaded file")?;
io::Seek::seek(f, io::SeekFrom::Start(resume_from))?;
let mut f = fs::File::open(src).chain_err(|| "unable to open downloaded file")?;
io::Seek::seek(&mut f, io::SeekFrom::Start(resume_from))?;

let ref mut buffer = vec![0u8; 0x10000];
let mut buffer = vec![0u8; 0x10000];
loop {
let bytes_read =
io::Read::read(f, buffer).chain_err(|| "unable to read downloaded file")?;
let bytes_read = io::Read::read(&mut f, &mut buffer)
.chain_err(|| "unable to read downloaded file")?;
if bytes_read == 0 {
break;
}
Expand Down
32 changes: 15 additions & 17 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn read_line() -> Result<String> {
lines
.next()
.and_then(|l| l.ok())
.ok_or("unable to read from stdin for confirmation".into())
.ok_or_else(|| "unable to read from stdin for confirmation".into())
}

pub fn set_globals(verbose: bool) -> Result<Cfg> {
Expand Down Expand Up @@ -146,8 +146,8 @@ fn show_channel_updates(
toolchains: Vec<(String, rustup::Result<UpdateStatus>)>,
) -> Result<()> {
let data = toolchains.into_iter().map(|(name, result)| {
let ref toolchain = cfg.get_toolchain(&name, false).expect("");
let version = rustc_version(toolchain);
let toolchain = cfg.get_toolchain(&name, false).expect("");
let version = rustc_version(&toolchain);

let banner;
let color;
Expand Down Expand Up @@ -195,7 +195,7 @@ fn show_channel_updates(
let _ = t.reset();
let _ = writeln!(t, " - {}", version);
}
let _ = writeln!(t, "");
let _ = writeln!(t);

Ok(())
}
Expand Down Expand Up @@ -355,20 +355,18 @@ pub fn list_toolchains(cfg: &Cfg) -> Result<()> {

if toolchains.is_empty() {
println!("no installed toolchains");
} else if let Ok(Some(def_toolchain)) = cfg.find_default() {
for toolchain in toolchains {
let if_default = if def_toolchain.name() == &*toolchain {
" (default)"
} else {
""
};
println!("{}{}", &toolchain, if_default);
}
} else {
if let Ok(Some(def_toolchain)) = cfg.find_default() {
for toolchain in toolchains {
let if_default = if def_toolchain.name() == &*toolchain {
" (default)"
} else {
""
};
println!("{}{}", &toolchain, if_default);
}
} else {
for toolchain in toolchains {
println!("{}", &toolchain);
}
for toolchain in toolchains {
println!("{}", &toolchain);
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/cli/download_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl DownloadTracker {
if self.displayed_progress {
// Display the finished state
self.display();
let _ = writeln!(self.term.as_mut().unwrap(), "");
let _ = writeln!(self.term.as_mut().unwrap());
}
self.prepare_for_new_download();
}
Expand Down
10 changes: 5 additions & 5 deletions src/cli/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn warn_fmt(args: fmt::Arguments<'_>) {
let _ = write!(t, "warning: ");
let _ = t.reset();
let _ = t.write_fmt(args);
let _ = write!(t, "\n");
let _ = writeln!(t);
}

pub fn err_fmt(args: fmt::Arguments<'_>) {
Expand All @@ -37,7 +37,7 @@ pub fn err_fmt(args: fmt::Arguments<'_>) {
let _ = write!(t, "error: ");
let _ = t.reset();
let _ = t.write_fmt(args);
let _ = write!(t, "\n");
let _ = writeln!(t);
}

pub fn info_fmt(args: fmt::Arguments<'_>) {
Expand All @@ -46,7 +46,7 @@ pub fn info_fmt(args: fmt::Arguments<'_>) {
let _ = write!(t, "info: ");
let _ = t.reset();
let _ = t.write_fmt(args);
let _ = write!(t, "\n");
let _ = writeln!(t);
}

pub fn verbose_fmt(args: fmt::Arguments<'_>) {
Expand All @@ -56,7 +56,7 @@ pub fn verbose_fmt(args: fmt::Arguments<'_>) {
let _ = write!(t, "verbose: ");
let _ = t.reset();
let _ = t.write_fmt(args);
let _ = write!(t, "\n");
let _ = writeln!(t);
}

pub fn debug_fmt(args: fmt::Arguments<'_>) {
Expand All @@ -67,6 +67,6 @@ pub fn debug_fmt(args: fmt::Arguments<'_>) {
let _ = write!(t, "verbose: ");
let _ = t.reset();
let _ = t.write_fmt(args);
let _ = write!(t, "\n");
let _ = writeln!(t);
}
}
4 changes: 2 additions & 2 deletions src/cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn main() -> Result<()> {
.as_ref()
.and_then(|a| a.file_name())
.and_then(|a| a.to_str());
let ref arg0 = arg0.ok_or(ErrorKind::NoExeName)?;
let arg0 = arg0.ok_or(ErrorKind::NoExeName)?;

// Check for a toolchain specifier.
let arg1 = args.next();
Expand All @@ -41,7 +41,7 @@ pub fn main() -> Result<()> {

let cfg = set_globals(false)?;
cfg.check_metadata_version()?;
direct_proxy(&cfg, arg0, toolchain, &cmd_args)?
direct_proxy(&cfg, &arg0, toolchain, &cmd_args)?
};

process::exit(c)
Expand Down
70 changes: 34 additions & 36 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ fn handle_epipe(res: Result<()>) -> Result<()> {
pub fn main() -> Result<()> {
crate::self_update::cleanup_self_updater()?;

let ref matches = cli().get_matches();
let matches = cli().get_matches();
let verbose = matches.is_present("verbose");
let ref cfg = common::set_globals(verbose)?;
let cfg = &common::set_globals(verbose)?;

if maybe_upgrade_data(cfg, matches)? {
if maybe_upgrade_data(cfg, &matches)? {
return Ok(());
}

Expand Down Expand Up @@ -411,7 +411,7 @@ pub fn cli() -> App<'static, 'static> {
)
.args(
&DOCS_DATA
.into_iter()
.iter()
.map(|(name, help_msg, _)| Arg::with_name(name).long(name).help(help_msg))
.collect::<Vec<_>>(),
)
Expand All @@ -424,7 +424,7 @@ pub fn cli() -> App<'static, 'static> {
.group(
ArgGroup::with_name("page").args(
&DOCS_DATA
.into_iter()
.iter()
.map(|(name, _, _)| *name)
.collect::<Vec<_>>(),
),
Expand Down Expand Up @@ -558,7 +558,7 @@ fn update_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
warn!("(partial) target triple specified instead of toolchain name");
let installed_toolchains = cfg.list_toolchains()?;
let default = cfg.find_default()?;
let default_name = default.map(|t| t.name().to_string()).unwrap_or("".into());
let default_name = default.map(|t| t.name().to_string()).unwrap_or_default();
let mut candidates = vec![];
for t in installed_toolchains {
if t == default_name {
Expand Down Expand Up @@ -606,11 +606,11 @@ fn default_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
if let Some(triple) = PartialTargetTriple::from_str(name) {
warn!("(partial) target triple specified instead of toolchain name");
let default = cfg.find_default()?;
let default_name = default.map(|t| t.name().to_string()).unwrap_or("".into());
let default_name = default.map(|t| t.name().to_string()).unwrap_or_default();
if let Ok(mut desc) = PartialToolchainDesc::from_str(&default_name) {
desc.target = triple;
let maybe_toolchain = format!("{}", desc);
let ref toolchain = cfg.get_toolchain(maybe_toolchain.as_ref(), false)?;
let toolchain = cfg.get_toolchain(maybe_toolchain.as_ref(), false)?;
if toolchain.name() == default_name {
warn!(
"(partial) triple '{}' resolves to a toolchain that is already default",
Expand All @@ -630,9 +630,9 @@ fn default_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {

fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
if m.is_present("toolchain") {
let ref toolchain = m.value_of("toolchain").expect("");
let toolchain = m.value_of("toolchain").expect("");
default_bare_triple_check(cfg, toolchain)?;
let ref toolchain = cfg.get_toolchain(toolchain, false)?;
let toolchain = cfg.get_toolchain(toolchain, false)?;

let status = if !toolchain.is_custom() {
Some(toolchain.install_from_dist_if_not_installed()?)
Expand Down Expand Up @@ -695,7 +695,7 @@ fn update(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
}

fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let toolchain = m.value_of("toolchain").expect("");
let args = m.values_of("command").unwrap();
let args: Vec<_> = args.collect();
let cmd = cfg.create_command_for_toolchain(toolchain, m.is_present("install"), args[0])?;
Expand Down Expand Up @@ -730,9 +730,9 @@ fn show(cfg: &Cfg) -> Result<()> {
writeln!(t)?;
}

let ref cwd = utils::current_dir()?;
let cwd = utils::current_dir()?;
let installed_toolchains = cfg.list_toolchains()?;
let active_toolchain = cfg.find_override_toolchain_or_default(cwd);
let active_toolchain = cfg.find_override_toolchain_or_default(&cwd);

// active_toolchain will carry the reason we don't have one in its detail.
let active_targets = if let Ok(ref at) = active_toolchain {
Expand Down Expand Up @@ -852,8 +852,8 @@ fn show(cfg: &Cfg) -> Result<()> {
}

fn show_active_toolchain(cfg: &Cfg) -> Result<()> {
let ref cwd = utils::current_dir()?;
if let Some((toolchain, reason)) = cfg.find_override_toolchain_or_default(cwd)? {
let cwd = utils::current_dir()?;
if let Some((toolchain, reason)) = cfg.find_override_toolchain_or_default(&cwd)? {
if reason.is_some() {
println!("{} ({})", toolchain.name(), reason.unwrap());
} else {
Expand Down Expand Up @@ -956,18 +956,20 @@ fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches<'_>) -> Result<Too
return Ok(toolchain);
}

let ref cwd = utils::current_dir()?;
let (toolchain, _) = cfg.toolchain_for_dir(cwd)?;
let cwd = utils::current_dir()?;
let (toolchain, _) = cfg.toolchain_for_dir(&cwd)?;

Ok(toolchain)
}

fn toolchain_link(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let ref path = m.value_of("path").expect("");
let toolchain = m.value_of("toolchain").expect("");
let path = m.value_of("path").expect("");
let toolchain = cfg.get_toolchain(toolchain, true)?;

Ok(toolchain.install_from_dir(Path::new(path), true)?)
toolchain
.install_from_dir(Path::new(path), true)
.map_err(|e| e.into())
}

fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Expand All @@ -979,7 +981,7 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
}

fn override_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let toolchain = m.value_of("toolchain").expect("");
let toolchain = cfg.get_toolchain(toolchain, false)?;

let status = if !toolchain.is_custom() {
Expand Down Expand Up @@ -1018,12 +1020,10 @@ fn override_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
info!("no nonexistent paths detected");
}
list
} else if m.is_present("path") {
vec![m.value_of("path").unwrap().to_string()]
} else {
if m.is_present("path") {
vec![m.value_of("path").unwrap().to_string()]
} else {
vec![utils::current_dir()?.to_str().unwrap().to_string()]
}
vec![utils::current_dir()?.to_str().unwrap().to_string()]
};

for path in paths {
Expand All @@ -1045,7 +1045,7 @@ fn override_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Ok(())
}

const DOCS_DATA: &[(&'static str, &'static str, &'static str,)] = &[
const DOCS_DATA: &[(&str, &str, &str,)] = &[
// flags can be used to open specific documents, e.g. `rustup doc --nomicon`
// tuple elements: document name used as flag, help message, document index path
("alloc", "The Rust core allocation and collections library", "alloc/index.html"),
Expand All @@ -1068,21 +1068,19 @@ const DOCS_DATA: &[(&'static str, &'static str, &'static str,)] = &[
fn doc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = explicit_or_dir_toolchain(cfg, m)?;

let doc_url = if let Some((_, _, path)) = DOCS_DATA
.into_iter()
.find(|(name, _, _)| m.is_present(name))
{
path
} else {
"index.html"
};
let doc_url =
if let Some((_, _, path)) = DOCS_DATA.iter().find(|(name, _, _)| m.is_present(name)) {
path
} else {
"index.html"
};

if m.is_present("path") {
let doc_path = toolchain.doc_path(doc_url)?;
println!("{}", doc_path.display());
Ok(())
} else {
Ok(toolchain.open_docs(doc_url)?)
toolchain.open_docs(doc_url).map_err(Into::into)
}
}

Expand Down
Loading

0 comments on commit cf07f2b

Please sign in to comment.