Rollup merge of - matthiaskrgr:wraps, r=petrochenkov

remove redundant option/result wrapping of return values

If a function always returns `Ok(something)`, we can return `something` directly and remove the corresponding error handling in the callers.
clippy::unnecessary_wraps
This commit is contained in:
Yuki Okushi 2021-02-22 18:26:10 +09:00 committed by GitHub
commit 1870b3bac6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 30 additions and 46 deletions
compiler
rustc_attr/src
rustc_codegen_ssa/src/back
rustc_expand/src
rustc_middle/src/mir/interpret
rustc_mir/src
interpret
transform/check_consts
rustc_parse/src/parser
rustc_passes/src

View file

@ -1035,14 +1035,14 @@ pub fn find_transparency(
pub fn allow_internal_unstable<'a>( pub fn allow_internal_unstable<'a>(
sess: &'a Session, sess: &'a Session,
attrs: &'a [Attribute], attrs: &'a [Attribute],
) -> Option<impl Iterator<Item = Symbol> + 'a> { ) -> impl Iterator<Item = Symbol> + 'a {
allow_unstable(sess, attrs, sym::allow_internal_unstable) allow_unstable(sess, attrs, sym::allow_internal_unstable)
} }
pub fn rustc_allow_const_fn_unstable<'a>( pub fn rustc_allow_const_fn_unstable<'a>(
sess: &'a Session, sess: &'a Session,
attrs: &'a [Attribute], attrs: &'a [Attribute],
) -> Option<impl Iterator<Item = Symbol> + 'a> { ) -> impl Iterator<Item = Symbol> + 'a {
allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable) allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
} }
@ -1050,7 +1050,7 @@ fn allow_unstable<'a>(
sess: &'a Session, sess: &'a Session,
attrs: &'a [Attribute], attrs: &'a [Attribute],
symbol: Symbol, symbol: Symbol,
) -> Option<impl Iterator<Item = Symbol> + 'a> { ) -> impl Iterator<Item = Symbol> + 'a {
let attrs = sess.filter_by_name(attrs, symbol); let attrs = sess.filter_by_name(attrs, symbol);
let list = attrs let list = attrs
.filter_map(move |attr| { .filter_map(move |attr| {
@ -1064,7 +1064,7 @@ fn allow_unstable<'a>(
}) })
.flatten(); .flatten();
Some(list.into_iter().filter_map(move |it| { list.into_iter().filter_map(move |it| {
let name = it.ident().map(|ident| ident.name); let name = it.ident().map(|ident| ident.name);
if name.is_none() { if name.is_none() {
sess.diagnostic().span_err( sess.diagnostic().span_err(
@ -1073,5 +1073,5 @@ fn allow_unstable<'a>(
); );
} }
name name
})) })
} }

View file

@ -735,7 +735,7 @@ fn execute_work_item<B: ExtraBackendMethods>(
match work_item { match work_item {
WorkItem::Optimize(module) => execute_optimize_work_item(cgcx, module, module_config), WorkItem::Optimize(module) => execute_optimize_work_item(cgcx, module, module_config),
WorkItem::CopyPostLtoArtifacts(module) => { WorkItem::CopyPostLtoArtifacts(module) => {
execute_copy_from_cache_work_item(cgcx, module, module_config) Ok(execute_copy_from_cache_work_item(cgcx, module, module_config))
} }
WorkItem::LTO(module) => execute_lto_work_item(cgcx, module, module_config), WorkItem::LTO(module) => execute_lto_work_item(cgcx, module, module_config),
} }
@ -844,7 +844,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
cgcx: &CodegenContext<B>, cgcx: &CodegenContext<B>,
module: CachedModuleCodegen, module: CachedModuleCodegen,
module_config: &ModuleConfig, module_config: &ModuleConfig,
) -> Result<WorkItemResult<B>, FatalError> { ) -> WorkItemResult<B> {
let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap(); let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();
let mut object = None; let mut object = None;
if let Some(saved_file) = module.source.saved_file { if let Some(saved_file) = module.source.saved_file {
@ -870,13 +870,13 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
assert_eq!(object.is_some(), module_config.emit_obj != EmitObj::None); assert_eq!(object.is_some(), module_config.emit_obj != EmitObj::None);
Ok(WorkItemResult::Compiled(CompiledModule { WorkItemResult::Compiled(CompiledModule {
name: module.name, name: module.name,
kind: ModuleKind::Regular, kind: ModuleKind::Regular,
object, object,
dwarf_object: None, dwarf_object: None,
bytecode: None, bytecode: None,
})) })
} }
fn execute_lto_work_item<B: ExtraBackendMethods>( fn execute_lto_work_item<B: ExtraBackendMethods>(

View file

@ -756,8 +756,8 @@ impl SyntaxExtension {
name: Symbol, name: Symbol,
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
) -> SyntaxExtension { ) -> SyntaxExtension {
let allow_internal_unstable = attr::allow_internal_unstable(sess, &attrs) let allow_internal_unstable =
.map(|features| features.collect::<Vec<Symbol>>().into()); Some(attr::allow_internal_unstable(sess, &attrs).collect::<Vec<Symbol>>().into());
let mut local_inner_macros = false; let mut local_inner_macros = false;
if let Some(macro_export) = sess.find_by_name(attrs, sym::macro_export) { if let Some(macro_export) = sess.find_by_name(attrs, sym::macro_export) {

View file

@ -266,7 +266,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
let range = self.check_bounds(ptr.offset, size); let range = self.check_bounds(ptr.offset, size);
self.mark_init(ptr, size, true); self.mark_init(ptr, size, true);
self.clear_relocations(cx, ptr, size)?; self.clear_relocations(cx, ptr, size);
AllocationExtra::memory_written(self, ptr, size)?; AllocationExtra::memory_written(self, ptr, size)?;
@ -484,18 +484,13 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
/// uninitialized. This is a somewhat odd "spooky action at a distance", /// uninitialized. This is a somewhat odd "spooky action at a distance",
/// but it allows strictly more code to run than if we would just error /// but it allows strictly more code to run than if we would just error
/// immediately in that case. /// immediately in that case.
fn clear_relocations( fn clear_relocations(&mut self, cx: &impl HasDataLayout, ptr: Pointer<Tag>, size: Size) {
&mut self,
cx: &impl HasDataLayout,
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
// Find the start and end of the given range and its outermost relocations. // Find the start and end of the given range and its outermost relocations.
let (first, last) = { let (first, last) = {
// Find all relocations overlapping the given range. // Find all relocations overlapping the given range.
let relocations = self.get_relocations(cx, ptr, size); let relocations = self.get_relocations(cx, ptr, size);
if relocations.is_empty() { if relocations.is_empty() {
return Ok(()); return;
} }
( (
@ -517,8 +512,6 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
// Forget all the relocations. // Forget all the relocations.
self.relocations.remove_range(first..last); self.relocations.remove_range(first..last);
Ok(())
} }
/// Errors if there are relocations overlapping with the edges of the /// Errors if there are relocations overlapping with the edges of the

View file

@ -23,11 +23,7 @@ use super::{
mod caller_location; mod caller_location;
mod type_name; mod type_name;
fn numeric_intrinsic<'tcx, Tag>( fn numeric_intrinsic<Tag>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Tag> {
name: Symbol,
bits: u128,
kind: Primitive,
) -> InterpResult<'tcx, Scalar<Tag>> {
let size = match kind { let size = match kind {
Primitive::Int(integer, _) => integer.size(), Primitive::Int(integer, _) => integer.size(),
_ => bug!("invalid `{}` argument: {:?}", name, bits), _ => bug!("invalid `{}` argument: {:?}", name, bits),
@ -41,7 +37,7 @@ fn numeric_intrinsic<'tcx, Tag>(
sym::bitreverse => (bits << extra).reverse_bits(), sym::bitreverse => (bits << extra).reverse_bits(),
_ => bug!("not a numeric intrinsic: {}", name), _ => bug!("not a numeric intrinsic: {}", name),
}; };
Ok(Scalar::from_uint(bits_out, size)) Scalar::from_uint(bits_out, size)
} }
/// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated /// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
@ -208,7 +204,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
if nonzero && bits == 0 { if nonzero && bits == 0 {
throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name); throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name);
} }
let out_val = numeric_intrinsic(intrinsic_name, bits, kind)?; let out_val = numeric_intrinsic(intrinsic_name, bits, kind);
self.write_scalar(out_val, dest)?; self.write_scalar(out_val, dest)?;
} }
sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => { sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {

View file

@ -85,8 +85,7 @@ pub fn rustc_allow_const_fn_unstable(
feature_gate: Symbol, feature_gate: Symbol,
) -> bool { ) -> bool {
let attrs = tcx.get_attrs(def_id); let attrs = tcx.get_attrs(def_id);
attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs) attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs).any(|name| name == feature_gate)
.map_or(false, |mut features| features.any(|name| name == feature_gate))
} }
// Returns `true` if the given `const fn` is "const-stable". // Returns `true` if the given `const fn` is "const-stable".

View file

@ -426,7 +426,7 @@ impl<'a> Parser<'a> {
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span); let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
let limits = let limits =
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed }; if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
Ok(self.mk_expr(span, self.mk_range(Some(lhs), rhs, limits)?, AttrVec::new())) Ok(self.mk_expr(span, self.mk_range(Some(lhs), rhs, limits), AttrVec::new()))
} }
fn is_at_start_of_range_notation_rhs(&self) -> bool { fn is_at_start_of_range_notation_rhs(&self) -> bool {
@ -474,7 +474,7 @@ impl<'a> Parser<'a> {
} else { } else {
(lo, None) (lo, None)
}; };
Ok(this.mk_expr(span, this.mk_range(None, opt_end, limits)?, attrs.into())) Ok(this.mk_expr(span, this.mk_range(None, opt_end, limits), attrs.into()))
}) })
} }
@ -1041,7 +1041,7 @@ impl<'a> Parser<'a> {
/// Assuming we have just parsed `.`, continue parsing into an expression. /// Assuming we have just parsed `.`, continue parsing into an expression.
fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> { fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
if self.token.uninterpolated_span().rust_2018() && self.eat_keyword(kw::Await) { if self.token.uninterpolated_span().rust_2018() && self.eat_keyword(kw::Await) {
return self.mk_await_expr(self_arg, lo); return Ok(self.mk_await_expr(self_arg, lo));
} }
let fn_span_lo = self.token.span; let fn_span_lo = self.token.span;
@ -2396,12 +2396,12 @@ impl<'a> Parser<'a> {
start: Option<P<Expr>>, start: Option<P<Expr>>,
end: Option<P<Expr>>, end: Option<P<Expr>>,
limits: RangeLimits, limits: RangeLimits,
) -> PResult<'a, ExprKind> { ) -> ExprKind {
if end.is_none() && limits == RangeLimits::Closed { if end.is_none() && limits == RangeLimits::Closed {
self.error_inclusive_range_with_no_end(self.prev_token.span); self.error_inclusive_range_with_no_end(self.prev_token.span);
Ok(ExprKind::Err) ExprKind::Err
} else { } else {
Ok(ExprKind::Range(start, end, limits)) ExprKind::Range(start, end, limits)
} }
} }
@ -2421,11 +2421,11 @@ impl<'a> Parser<'a> {
ExprKind::Call(f, args) ExprKind::Call(f, args)
} }
fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> { fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> P<Expr> {
let span = lo.to(self.prev_token.span); let span = lo.to(self.prev_token.span);
let await_expr = self.mk_expr(span, ExprKind::Await(self_arg), AttrVec::new()); let await_expr = self.mk_expr(span, ExprKind::Await(self_arg), AttrVec::new());
self.recover_from_await_method_call(); self.recover_from_await_method_call();
Ok(await_expr) await_expr
} }
crate fn mk_expr(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> { crate fn mk_expr(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> {

View file

@ -1679,7 +1679,7 @@ impl<'a> Parser<'a> {
let constness = self.parse_constness(); let constness = self.parse_constness();
let asyncness = self.parse_asyncness(); let asyncness = self.parse_asyncness();
let unsafety = self.parse_unsafety(); let unsafety = self.parse_unsafety();
let ext = self.parse_extern()?; let ext = self.parse_extern();
if let Async::Yes { span, .. } = asyncness { if let Async::Yes { span, .. } = asyncness {
self.ban_async_in_2015(span); self.ban_async_in_2015(span);

View file

@ -1202,12 +1202,8 @@ impl<'a> Parser<'a> {
} }
/// Parses `extern string_literal?`. /// Parses `extern string_literal?`.
fn parse_extern(&mut self) -> PResult<'a, Extern> { fn parse_extern(&mut self) -> Extern {
Ok(if self.eat_keyword(kw::Extern) { if self.eat_keyword(kw::Extern) { Extern::from_abi(self.parse_abi()) } else { Extern::None }
Extern::from_abi(self.parse_abi())
} else {
Extern::None
})
} }
/// Parses a string literal as an ABI spec. /// Parses a string literal as an ABI spec.

View file

@ -106,7 +106,7 @@ impl<'tcx> CheckConstVisitor<'tcx> {
// However, we cannot allow stable `const fn`s to use unstable features without an explicit // However, we cannot allow stable `const fn`s to use unstable features without an explicit
// opt-in via `rustc_allow_const_fn_unstable`. // opt-in via `rustc_allow_const_fn_unstable`.
attr::rustc_allow_const_fn_unstable(&tcx.sess, &tcx.get_attrs(def_id)) attr::rustc_allow_const_fn_unstable(&tcx.sess, &tcx.get_attrs(def_id))
.map_or(false, |mut features| features.any(|name| name == feature_gate)) .any(|name| name == feature_gate)
}; };
match required_gates { match required_gates {