Skip to content

Commit e278ea3

Browse files
committed
Auto merge of #3733 - llogiq:clippy, r=alexcrichton
some clippy-suggested improvements This fixes a number of [clippy](https://github.com/Manishearth/rust-clippy) warnings. It's mostly about readability, though a few changes could affect performance (though probably not measurably). I've left out things to fix later; I thought I'd just push the first batch to see if you like it.
2 parents 541a3da + 1e50878 commit e278ea3

34 files changed

+165
-198
lines changed

src/cargo/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ impl fmt::Display for VersionInfo {
7777
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7878
write!(f, "cargo {}.{}.{}",
7979
self.major, self.minor, self.patch)?;
80-
match self.cfg_info.as_ref().map(|ci| &ci.release_channel) {
81-
Some(channel) => {
82-
if channel != "stable" {
83-
write!(f, "-{}", channel)?;
84-
let empty = String::from("");
85-
write!(f, "{}", self.pre_release.as_ref().unwrap_or(&empty))?;
86-
}
87-
},
88-
None => (),
80+
if let Some(channel) = self.cfg_info.as_ref().map(|ci| &ci.release_channel) {
81+
if channel != "stable" {
82+
write!(f, "-{}", channel)?;
83+
let empty = String::from("");
84+
write!(f, "{}", self.pre_release.as_ref().unwrap_or(&empty))?;
85+
}
8986
};
9087

9188
if let Some(ref cfg) = self.cfg_info {

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,13 @@ pub fn parse_dep_info(dep_info: &Path) -> CargoResult<Option<Vec<PathBuf>>> {
583583

584584
let mut paths = Vec::new();
585585
let mut deps = deps.split(' ').map(|s| s.trim()).filter(|s| !s.is_empty());
586-
loop {
587-
let mut file = match deps.next() {
588-
Some(s) => s.to_string(),
589-
None => break,
590-
};
591-
while file.ends_with("\\") {
586+
while let Some(s) = deps.next() {
587+
let mut file = s.to_string();
588+
while file.ends_with('\\') {
592589
file.pop();
593590
file.push(' ');
594591
file.push_str(deps.next().chain_error(|| {
595-
internal(format!("malformed dep-info format, trailing \\"))
592+
internal("malformed dep-info format, trailing \\".to_string())
596593
})?);
597594
}
598595
paths.push(cwd.join(&file));
@@ -602,7 +599,7 @@ pub fn parse_dep_info(dep_info: &Path) -> CargoResult<Option<Vec<PathBuf>>> {
602599

603600
fn dep_info_mtime_if_fresh(dep_info: &Path) -> CargoResult<Option<FileTime>> {
604601
if let Some(paths) = parse_dep_info(dep_info)? {
605-
Ok(mtime_if_fresh(&dep_info, paths.iter()))
602+
Ok(mtime_if_fresh(dep_info, paths.iter()))
606603
} else {
607604
Ok(None)
608605
}

src/cargo/ops/cargo_rustc/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Layout {
7676
// the target triple as a Path and then just use the file stem as the
7777
// component for the directory name.
7878
if let Some(triple) = triple {
79-
path.push(Path::new(triple).file_stem().ok_or(human(format!("target was empty")))?);
79+
path.push(Path::new(triple).file_stem().ok_or(human("target was empty".to_string()))?);
8080
}
8181
path.push(dest);
8282
Layout::at(ws.config(), path)

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn compile_targets<'a, 'cfg: 'a>(ws: &Workspace<'cfg>,
170170
let pkgid = unit.pkg.package_id();
171171
if !unit.target.is_lib() { continue }
172172
if unit.profile.doc { continue }
173-
if cx.compilation.libraries.contains_key(&pkgid) {
173+
if cx.compilation.libraries.contains_key(pkgid) {
174174
continue
175175
}
176176

@@ -182,9 +182,9 @@ pub fn compile_targets<'a, 'cfg: 'a>(ws: &Workspace<'cfg>,
182182
}
183183
}
184184

185-
if let Some(feats) = cx.resolve.features(&unit.pkg.package_id()) {
185+
if let Some(feats) = cx.resolve.features(unit.pkg.package_id()) {
186186
cx.compilation.cfgs.entry(unit.pkg.package_id().clone())
187-
.or_insert(HashSet::new())
187+
.or_insert_with(HashSet::new)
188188
.extend(feats.iter().map(|feat| format!("feature=\"{}\"", feat)));
189189
}
190190

@@ -193,7 +193,7 @@ pub fn compile_targets<'a, 'cfg: 'a>(ws: &Workspace<'cfg>,
193193

194194
for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() {
195195
cx.compilation.cfgs.entry(pkg.clone())
196-
.or_insert(HashSet::new())
196+
.or_insert_with(HashSet::new)
197197
.extend(output.cfgs.iter().cloned());
198198

199199
for dir in output.library_paths.iter() {
@@ -344,7 +344,7 @@ fn rustc(cx: &mut Context, unit: &Unit, exec: Arc<Executor>) -> CargoResult<Work
344344
},
345345
&mut |line| {
346346
// stderr from rustc can have a mix of JSON and non-JSON output
347-
if line.starts_with("{") {
347+
if line.starts_with('{') {
348348
// Handle JSON lines
349349
let compiler_message = json::Json::from_str(line).map_err(|_| {
350350
internal(&format!("compiler produced invalid json: `{}`", line))

src/cargo/ops/cargo_rustc/output_depinfo.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn render_filename<P: AsRef<Path>>(path: P, basedir: Option<&str>) -> CargoResul
2222
fn add_deps_for_unit<'a, 'b>(deps: &mut HashSet<PathBuf>, context: &mut Context<'a, 'b>,
2323
unit: &Unit<'a>, visited: &mut HashSet<Unit<'a>>) -> CargoResult<()>
2424
{
25-
if !visited.insert(unit.clone()) {
25+
if !visited.insert(*unit) {
2626
return Ok(());
2727
}
2828

@@ -76,13 +76,10 @@ pub fn output_depinfo<'a, 'b>(context: &mut Context<'a, 'b>, unit: &Unit<'a>) ->
7676
// dep-info generation failed, so delete output file. This will usually
7777
// cause the build system to always rerun the build rule, which is correct
7878
// if inefficient.
79-
match fs::remove_file(output_path) {
80-
Err(err) => {
81-
if err.kind() != ErrorKind::NotFound {
82-
return Err(err.into());
83-
}
79+
if let Err(err) = fs::remove_file(output_path) {
80+
if err.kind() != ErrorKind::NotFound {
81+
return Err(err.into());
8482
}
85-
_ => ()
8683
}
8784
}
8885
}

src/cargo/ops/cargo_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn run_unit_tests(options: &TestOptions,
8585
let mut errors = Vec::new();
8686

8787
for &(ref pkg, _, ref exe) in &compilation.tests {
88-
let to_display = match util::without_prefix(exe, &cwd) {
88+
let to_display = match util::without_prefix(exe, cwd) {
8989
Some(path) => path,
9090
None => &**exe,
9191
};
@@ -145,7 +145,7 @@ fn run_doc_tests(options: &TestOptions,
145145
p.arg("--test-args").arg(arg);
146146
}
147147

148-
if let Some(cfgs) = compilation.cfgs.get(&package.package_id()) {
148+
if let Some(cfgs) = compilation.cfgs.get(package.package_id()) {
149149
for cfg in cfgs.iter() {
150150
p.arg("--cfg").arg(cfg);
151151
}

src/cargo/ops/lockfile.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,17 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
6767
emit_package(root.as_table().unwrap(), &mut out);
6868
}
6969

70-
let deps = e.toml.get(&"package".to_string()).unwrap().as_slice().unwrap();
70+
let deps = e.toml[&"package".to_string()].as_slice().unwrap();
7171
for dep in deps.iter() {
7272
let dep = dep.as_table().unwrap();
7373

7474
out.push_str("[[package]]\n");
7575
emit_package(dep, &mut out);
7676
}
7777

78-
match e.toml.get(&"metadata".to_string()) {
79-
Some(metadata) => {
80-
out.push_str("[metadata]\n");
81-
out.push_str(&metadata.to_string());
82-
}
83-
None => {}
78+
if let Some(metadata) = e.toml.get(&"metadata".to_string()) {
79+
out.push_str("[metadata]\n");
80+
out.push_str(&metadata.to_string());
8481
}
8582

8683
// If the lockfile contents haven't changed so don't rewrite it. This is
@@ -128,8 +125,8 @@ fn emit_package(dep: &toml::Table, out: &mut String) {
128125
out.push_str(&format!("source = {}\n", lookup(dep, "source")));
129126
}
130127

131-
if let Some(ref s) = dep.get("dependencies") {
132-
let slice = Value::as_slice(*s).unwrap();
128+
if let Some(s) = dep.get("dependencies") {
129+
let slice = Value::as_slice(s).unwrap();
133130

134131
if !slice.is_empty() {
135132
out.push_str("dependencies = [\n");

src/cargo/ops/registry.rs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
5151
let (mut registry, reg_id) = registry(opts.config,
5252
opts.token.clone(),
5353
opts.index.clone())?;
54-
verify_dependencies(&pkg, &reg_id)?;
54+
verify_dependencies(pkg, &reg_id)?;
5555

5656
// Prepare a tarball, with a non-surpressable warning if metadata
5757
// is missing since this is being put online.
@@ -66,7 +66,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
6666

6767
// Upload said tarball to the specified destination
6868
opts.config.shell().status("Uploading", pkg.package_id().to_string())?;
69-
transmit(opts.config, &pkg, tarball.file(), &mut registry, opts.dry_run)?;
69+
transmit(opts.config, pkg, tarball.file(), &mut registry, opts.dry_run)?;
7070

7171
Ok(())
7272
}
@@ -121,13 +121,10 @@ fn transmit(config: &Config,
121121
Some(ref readme) => Some(paths::read(&pkg.root().join(readme))?),
122122
None => None,
123123
};
124-
match *license_file {
125-
Some(ref file) => {
126-
if fs::metadata(&pkg.root().join(file)).is_err() {
127-
bail!("the license file `{}` does not exist", file)
128-
}
124+
if let Some(ref file) = *license_file {
125+
if fs::metadata(&pkg.root().join(file)).is_err() {
126+
bail!("the license file `{}` does not exist", file)
129127
}
130-
None => {}
131128
}
132129

133130
// Do not upload if performing a dry run
@@ -246,18 +243,13 @@ pub fn http_handle(config: &Config) -> CargoResult<Easy> {
246243
/// Favor cargo's `http.proxy`, then git's `http.proxy`. Proxies specified
247244
/// via environment variables are picked up by libcurl.
248245
fn http_proxy(config: &Config) -> CargoResult<Option<String>> {
249-
match config.get_string("http.proxy")? {
250-
Some(s) => return Ok(Some(s.val)),
251-
None => {}
246+
if let Some(s) = config.get_string("http.proxy")? {
247+
return Ok(Some(s.val))
252248
}
253-
match git2::Config::open_default() {
254-
Ok(cfg) => {
255-
match cfg.get_str("http.proxy") {
256-
Ok(s) => return Ok(Some(s.to_string())),
257-
Err(..) => {}
258-
}
249+
if let Ok(cfg) = git2::Config::open_default() {
250+
if let Ok(s) = cfg.get_str("http.proxy") {
251+
return Ok(Some(s.to_string()))
259252
}
260-
Err(..) => {}
261253
}
262254
Ok(None)
263255
}
@@ -282,9 +274,8 @@ pub fn http_proxy_exists(config: &Config) -> CargoResult<bool> {
282274
}
283275

284276
pub fn http_timeout(config: &Config) -> CargoResult<Option<i64>> {
285-
match config.get_i64("http.timeout")? {
286-
Some(s) => return Ok(Some(s.val)),
287-
None => {}
277+
if let Some(s) = config.get_i64("http.timeout")? {
278+
return Ok(Some(s.val))
288279
}
289280
Ok(env::var("HTTP_TIMEOUT").ok().and_then(|s| s.parse().ok()))
290281
}
@@ -293,11 +284,8 @@ pub fn registry_login(config: &Config, token: String) -> CargoResult<()> {
293284
let RegistryConfig { index, token: _ } = registry_configuration(config)?;
294285
let mut map = HashMap::new();
295286
let p = config.cwd().to_path_buf();
296-
match index {
297-
Some(index) => {
298-
map.insert("index".to_string(), ConfigValue::String(index, p.clone()));
299-
}
300-
None => {}
287+
if let Some(index) = index {
288+
map.insert("index".to_string(), ConfigValue::String(index, p.clone()));
301289
}
302290
map.insert("token".to_string(), ConfigValue::String(token, p));
303291

@@ -327,28 +315,22 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
327315
let (mut registry, _) = registry(config, opts.token.clone(),
328316
opts.index.clone())?;
329317

330-
match opts.to_add {
331-
Some(ref v) => {
332-
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
333-
config.shell().status("Owner", format!("adding {:?} to crate {}",
334-
v, name))?;
335-
registry.add_owners(&name, &v).map_err(|e| {
336-
human(format!("failed to add owners to crate {}: {}", name, e))
337-
})?;
338-
}
339-
None => {}
318+
if let Some(ref v) = opts.to_add {
319+
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
320+
config.shell().status("Owner", format!("adding {:?} to crate {}",
321+
v, name))?;
322+
registry.add_owners(&name, &v).map_err(|e| {
323+
human(format!("failed to add owners to crate {}: {}", name, e))
324+
})?;
340325
}
341326

342-
match opts.to_remove {
343-
Some(ref v) => {
344-
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
345-
config.shell().status("Owner", format!("removing {:?} from crate {}",
346-
v, name))?;
347-
registry.remove_owners(&name, &v).map_err(|e| {
348-
human(format!("failed to remove owners from crate {}: {}", name, e))
349-
})?;
350-
}
351-
None => {}
327+
if let Some(ref v) = opts.to_remove {
328+
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
329+
config.shell().status("Owner", format!("removing {:?} from crate {}",
330+
v, name))?;
331+
registry.remove_owners(&name, &v).map_err(|e| {
332+
human(format!("failed to remove owners from crate {}: {}", name, e))
333+
})?;
352334
}
353335

354336
if opts.list {

src/cargo/ops/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn resolve_ws_precisely<'a>(ws: &Workspace<'a>,
6161
let resolved_with_overrides =
6262
ops::resolve_with_previous(&mut registry, ws,
6363
method, Some(&resolve), None,
64-
&specs)?;
64+
specs)?;
6565

6666
for &(ref replace_spec, _) in ws.root_replace() {
6767
if !resolved_with_overrides.replacements().keys().any(|r| replace_spec.matches(r)) {

src/cargo/sources/directory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'cfg> Source for DirectorySource<'cfg> {
7676
// crates and otherwise may conflict with a VCS
7777
// (rust-lang/cargo#3414).
7878
if let Some(s) = path.file_name().and_then(|s| s.to_str()) {
79-
if s.starts_with(".") {
79+
if s.starts_with('.') {
8080
continue
8181
}
8282
}

src/cargo/sources/git/source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'cfg> Source for GitSource<'cfg> {
146146

147147
trace!("updating git source `{:?}`", self.remote);
148148

149-
let repo = self.remote.checkout(&db_path, &self.config)?;
149+
let repo = self.remote.checkout(&db_path, self.config)?;
150150
let rev = repo.rev_for(&self.reference)?;
151151
(repo, rev)
152152
} else {
@@ -166,7 +166,7 @@ impl<'cfg> Source for GitSource<'cfg> {
166166
// in scope so the destructors here won't tamper with too much.
167167
// Checkout is immutable, so we don't need to protect it with a lock once
168168
// it is created.
169-
repo.copy_to(actual_rev.clone(), &checkout_path, &self.config)?;
169+
repo.copy_to(actual_rev.clone(), &checkout_path, self.config)?;
170170

171171
let source_id = self.source_id.with_precise(Some(actual_rev.to_string()));
172172
let path_source = PathSource::new_recursive(&checkout_path,

0 commit comments

Comments
 (0)