Skip to content

Commit

Permalink
Rollup merge of rust-lang#69802 - matthiaskrgr:cl1ppy, r=Dylan-DPC
Browse files Browse the repository at this point in the history
fix more clippy findings

* reduce references on match patterns (clippy::match_ref_pats)
* Use writeln!(fmt, "word") instead of write!(fmt, "word\n") (clippy::write_with_newline)
* libtest: remove redundant argument to writeln!() (clippy::writeln_empty_string)
* remove unneeded mutable references (cippy::unnecessary_mut_passed)
* libtest: declare variables as floats instead of casting them (clippy::unnecessary_cast)
* rustdoc: remove redundant static lifetimes (clippy::redundant_static_lifetimes)
* call .as_deref() instead of .as_ref().map(Deref::deref) (clippy::option_as_ref_deref)
* iterate over a maps values directly. (clippy::for_kv_map)
* rustdoc: simplify boolean condition (clippy::nonminimal_bool)
* Use ?-operator in more places (clippy::question_mark, had some false negatives fixed recently)
* rustdoc: Use .any(p) instead of find(p).is_some(). (clippy::search_is_some)
* rustdoc: don't call into_iter() on iterator. (clippy::identity_conversion)
  • Loading branch information
Centril committed Mar 13, 2020
2 parents 8a68afc + 8351138 commit 881b87b
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 118 deletions.
13 changes: 5 additions & 8 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,7 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
let haystack = self.haystack.as_bytes();
loop {
// get the haystack up to but not including the last character searched
let bytes = if let Some(slice) = haystack.get(self.finger..self.finger_back) {
slice
} else {
return None;
};
let bytes = haystack.get(self.finger..self.finger_back)?;
// the last byte of the utf8 encoded needle
// SAFETY: we have an invariant that `utf8_size < 5`
let last_byte = unsafe { *self.utf8_encoded.get_unchecked(self.utf8_size - 1) };
Expand Down Expand Up @@ -575,11 +571,12 @@ macro_rules! pattern_methods {

#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool
where $t: ReverseSearcher<'a>
where
$t: ReverseSearcher<'a>,
{
($pmap)(self).is_suffix_of(haystack)
}
}
};
}

macro_rules! searcher_methods {
Expand Down Expand Up @@ -614,7 +611,7 @@ macro_rules! searcher_methods {
fn next_reject_back(&mut self) -> Option<(usize, usize)> {
self.0.next_reject_back()
}
}
};
}

/////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'hir> Map<'hir> {
}

pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
let node = if let Some(node) = self.find(hir_id) { node } else { return None };
let node = self.find(hir_id)?;

Some(match node {
Node::Item(item) => match item.kind {
Expand Down
7 changes: 1 addition & 6 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,7 @@ impl<'tcx> TyCtxt<'tcx> {
adt_did: DefId,
validate: &mut dyn FnMut(Self, DefId) -> Result<(), ErrorReported>,
) -> Option<ty::Destructor> {
let drop_trait = if let Some(def_id) = self.lang_items().drop_trait() {
def_id
} else {
return None;
};

let drop_trait = self.lang_items().drop_trait()?;
self.ensure().coherent_trait(drop_trait);

let mut dtor_did = None;
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
return None;
}

let matches = if let Some(matches) = handle_options(&args) {
matches
} else {
return None;
};

let matches = handle_options(&args)?;
let mut result = Vec::new();
let mut excluded_cargo_defaults = false;
for flag in ICE_REPORT_COMPILER_FLAGS {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_infer/traits/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
return AutoTraitResult::ExplicitImpl;
}

return tcx.infer_ctxt().enter(|mut infcx| {
return tcx.infer_ctxt().enter(|infcx| {
let mut fresh_preds = FxHashSet::default();

// Due to the way projections are handled by SelectionContext, we need to run
Expand Down Expand Up @@ -164,7 +164,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {

let (full_env, full_user_env) = self
.evaluate_predicates(
&mut infcx,
&infcx,
trait_did,
ty,
new_env,
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_infer/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,7 @@ pub(super) fn specialization_graph_provider(
fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<String> {
use std::fmt::Write;

let trait_ref = if let Some(tr) = tcx.impl_trait_ref(impl_def_id) {
tr
} else {
return None;
};

let trait_ref = tcx.impl_trait_ref(impl_def_id)?;
let mut w = "impl".to_owned();

let substs = InternalSubsts::identity_for_item(tcx, impl_def_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ fn create_generator_drop_shim<'tcx>(
// unrelated code from the resume part of the function
simplify::remove_dead_blocks(&mut body);

dump_mir(tcx, None, "generator_drop", &0, source, &mut body, |_, _| Ok(()));
dump_mir(tcx, None, "generator_drop", &0, source, &body, |_, _| Ok(()));

body
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/util/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn dump_matched_mir_node<'tcx>(
writeln!(file, "// MIR local liveness analysis for `{}`", node_path)?;
writeln!(file, "// source = {:?}", source)?;
writeln!(file, "// pass_name = {}", pass_name)?;
writeln!(file, "")?;
writeln!(file)?;
write_mir_fn(tcx, source, body, &mut file, result)?;
Ok(())
});
Expand All @@ -316,7 +316,7 @@ pub fn write_mir_fn<'tcx>(
write_basic_block(tcx, block, body, &mut |_, _| Ok(()), w)?;
print(w, " ", &result.outs)?;
if block.index() + 1 != body.basic_blocks().len() {
writeln!(w, "")?;
writeln!(w)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn dump_matched_mir_node<'tcx, F>(
if let Some(ref layout) = body.generator_layout {
writeln!(file, "// generator_layout = {:?}", layout)?;
}
writeln!(file, "")?;
writeln!(file)?;
extra_data(PassWhere::BeforeCFG, &mut file)?;
write_user_type_annotations(body, &mut file)?;
write_mir_fn(tcx, source, body, &mut extra_data, &mut file)?;
Expand Down Expand Up @@ -242,13 +242,13 @@ pub fn write_mir_pretty<'tcx>(
first = false;
} else {
// Put empty lines between all items
writeln!(w, "")?;
writeln!(w)?;
}

write_mir_fn(tcx, MirSource::item(def_id), body, &mut |_, _| Ok(()), w)?;

for (i, body) in tcx.promoted_mir(def_id).iter_enumerated() {
writeln!(w, "")?;
writeln!(w)?;
let src = MirSource { instance: ty::InstanceDef::Item(def_id), promoted: Some(i) };
write_mir_fn(tcx, src, body, &mut |_, _| Ok(()), w)?;
}
Expand All @@ -271,7 +271,7 @@ where
extra_data(PassWhere::BeforeBlock(block), w)?;
write_basic_block(tcx, block, body, extra_data, w)?;
if block.index() + 1 != body.basic_blocks().len() {
writeln!(w, "")?;
writeln!(w)?;
}
}

Expand Down Expand Up @@ -529,7 +529,7 @@ pub fn write_mir_intro<'tcx>(
write_scope_tree(tcx, body, &scope_tree, w, OUTERMOST_SOURCE_SCOPE, 1)?;

// Add an empty line before the first block is printed.
writeln!(w, "")?;
writeln!(w)?;

Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
}
};

match (
lifetime_names.len(),
lifetime_names.iter().next(),
snippet.as_ref().map(|s| s.as_str()),
) {
match (lifetime_names.len(), lifetime_names.iter().next(), snippet.as_deref()) {
(1, Some(name), Some("&")) => {
suggest_existing(err, format!("&{} ", name));
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2773,12 +2773,8 @@ impl<'a> Resolver<'a> {
} else {
let crate_id = if !speculative {
self.crate_loader.process_path_extern(ident.name, ident.span)
} else if let Some(crate_id) =
self.crate_loader.maybe_process_path_extern(ident.name, ident.span)
{
crate_id
} else {
return None;
self.crate_loader.maybe_process_path_extern(ident.name, ident.span)?
};
let crate_root = self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
Some(
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,11 +1147,7 @@ impl SourceFile {
}

let begin = {
let line = if let Some(line) = self.lines.get(line_number) {
line
} else {
return None;
};
let line = self.lines.get(line_number)?;
let begin: BytePos = *line - self.start_pos;
begin.to_usize()
};
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ pub fn try_inline(
attrs: Option<Attrs<'_>>,
visited: &mut FxHashSet<DefId>,
) -> Option<Vec<clean::Item>> {
let did = if let Some(did) = res.opt_def_id() {
did
} else {
return None;
};
let did = res.opt_def_id()?;
if did.is_local() {
return None;
}
Expand Down Expand Up @@ -578,7 +574,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
name: ref _name,
},
ref bounds,
} => !(*s == "Self" && did == trait_did) && !bounds.is_empty(),
} => !(bounds.is_empty() || *s == "Self" && did == trait_did),
_ => true,
});
g
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,7 @@ pub fn plain_summary_line(md: &str) -> String {
type Item = String;

fn next(&mut self) -> Option<String> {
let next_event = self.inner.next();
if next_event.is_none() {
return None;
}
let next_event = next_event.unwrap();
let next_event = self.inner.next()?;
let (ret, is_in) = match next_event {
Event::Start(Tag::Paragraph) => (None, 1),
Event::Start(Tag::Heading(_)) => (None, 1),
Expand All @@ -870,7 +866,7 @@ pub fn plain_summary_line(md: &str) -> String {
}
let mut s = String::with_capacity(md.len() * 3 / 2);
let p = ParserWrapper { inner: Parser::new(md), is_in: 0, is_first: true };
p.into_iter().filter(|t| !t.is_empty()).for_each(|i| s.push_str(&i));
p.filter(|t| !t.is_empty()).for_each(|i| s.push_str(&i));
s
}

Expand Down
10 changes: 4 additions & 6 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ impl Context {
}

if self.shared.sort_modules_alphabetically {
for (_, items) in &mut map {
for items in map.values_mut() {
items.sort();
}
}
Expand Down Expand Up @@ -3396,10 +3396,8 @@ fn render_assoc_items(
let deref_impl =
traits.iter().find(|t| t.inner_impl().trait_.def_id() == c.deref_trait_did);
if let Some(impl_) = deref_impl {
let has_deref_mut = traits
.iter()
.find(|t| t.inner_impl().trait_.def_id() == c.deref_mut_trait_did)
.is_some();
let has_deref_mut =
traits.iter().any(|t| t.inner_impl().trait_.def_id() == c.deref_mut_trait_did);
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
}

Expand Down Expand Up @@ -3740,7 +3738,7 @@ fn render_impl(
) {
for trait_item in &t.items {
let n = trait_item.name.clone();
if i.items.iter().find(|m| m.name == n).is_some() {
if i.items.iter().any(|m| m.name == n) {
continue;
}
let did = i.trait_.as_ref().unwrap().def_id().unwrap();
Expand Down
Loading

0 comments on commit 881b87b

Please sign in to comment.