From d6fa011f62ee0c3b256d0be9a69369cbceb3c45b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 30 Jul 2020 11:15:51 +1000 Subject: [PATCH] Remove some unnecessary uses of `Option`. These arguments are never `None`. --- src/librustc_attr/builtin.rs | 31 ++++++++++++---------- src/librustc_driver/lib.rs | 2 +- src/librustc_interface/queries.rs | 2 +- src/librustc_metadata/creader.rs | 2 +- src/librustc_mir_build/build/mod.rs | 2 +- src/librustc_session/output.rs | 40 +++++++++++++---------------- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/librustc_attr/builtin.rs b/src/librustc_attr/builtin.rs index 983202aafab..ddf8fc23779 100644 --- a/src/librustc_attr/builtin.rs +++ b/src/librustc_attr/builtin.rs @@ -86,7 +86,7 @@ pub enum UnwindAttr { } /// Determine what `#[unwind]` attribute is present in `attrs`, if any. -pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Option { +pub fn find_unwind_attr(diagnostic: &Handler, attrs: &[Attribute]) -> Option { attrs.iter().fold(None, |ia, attr| { if attr.check_name(sym::unwind) { if let Some(meta) = attr.meta() { @@ -99,19 +99,22 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op } } - if let Some(d) = diagnostic { - struct_span_err!(d, attr.span, E0633, "malformed `unwind` attribute input") - .span_label(attr.span, "invalid argument") - .span_suggestions( - attr.span, - "the allowed arguments are `allowed` and `aborts`", - (vec!["allowed", "aborts"]) - .into_iter() - .map(|s| format!("#[unwind({})]", s)), - Applicability::MachineApplicable, - ) - .emit(); - }; + struct_span_err!( + diagnostic, + attr.span, + E0633, + "malformed `unwind` attribute input" + ) + .span_label(attr.span, "invalid argument") + .span_suggestions( + attr.span, + "the allowed arguments are `allowed` and `aborts`", + (vec!["allowed", "aborts"]) + .into_iter() + .map(|s| format!("#[unwind({})]", s)), + Applicability::MachineApplicable, + ) + .emit(); } } } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 7af640c109e..cc954cee907 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -677,7 +677,7 @@ impl RustcDefaultCalls { let t_outputs = rustc_interface::util::build_output_filenames( input, odir, ofile, attrs, sess, ); - let id = rustc_session::output::find_crate_name(Some(sess), attrs, input); + let id = rustc_session::output::find_crate_name(sess, attrs, input); if *req == PrintRequest::CrateName { println!("{}", id); continue; diff --git a/src/librustc_interface/queries.rs b/src/librustc_interface/queries.rs index 4265e6dca6a..4ee8cb34cc0 100644 --- a/src/librustc_interface/queries.rs +++ b/src/librustc_interface/queries.rs @@ -159,7 +159,7 @@ impl<'tcx> Queries<'tcx> { None => { let parse_result = self.parse()?; let krate = parse_result.peek(); - find_crate_name(Some(self.session()), &krate.attrs, &self.compiler.input) + find_crate_name(self.session(), &krate.attrs, &self.compiler.input) } }) }) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 9bc6c054e4d..16748c42d30 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -895,7 +895,7 @@ impl<'a> CrateLoader<'a> { ); let name = match orig_name { Some(orig_name) => { - validate_crate_name(Some(self.sess), &orig_name.as_str(), Some(item.span)); + validate_crate_name(self.sess, &orig_name.as_str(), Some(item.span)); orig_name } None => item.ident.name, diff --git a/src/librustc_mir_build/build/mod.rs b/src/librustc_mir_build/build/mod.rs index 3c4587119cd..3894472e7d5 100644 --- a/src/librustc_mir_build/build/mod.rs +++ b/src/librustc_mir_build/build/mod.rs @@ -537,7 +537,7 @@ macro_rules! unpack { fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, _abi: Abi) -> bool { // Validate `#[unwind]` syntax regardless of platform-specific panic strategy. let attrs = &tcx.get_attrs(fn_def_id.to_def_id()); - let unwind_attr = attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs); + let unwind_attr = attr::find_unwind_attr(tcx.sess.diagnostic(), attrs); // We never unwind, so it's not relevant to stop an unwind. if tcx.sess.panic_strategy() != PanicStrategy::Unwind { diff --git a/src/librustc_session/output.rs b/src/librustc_session/output.rs index 52216188397..994f06a7c6e 100644 --- a/src/librustc_session/output.rs +++ b/src/librustc_session/output.rs @@ -45,7 +45,7 @@ fn is_writeable(p: &Path) -> bool { } } -pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: &Input) -> String { +pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input) -> String { let validate = |s: String, span: Option| { validate_crate_name(sess, &s, span); s @@ -58,25 +58,24 @@ pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: let attr_crate_name = attr::find_by_name(attrs, sym::crate_name).and_then(|at| at.value_str().map(|s| (at, s))); - if let Some(sess) = sess { - if let Some(ref s) = sess.opts.crate_name { - if let Some((attr, name)) = attr_crate_name { - if name.as_str() != *s { - let msg = format!( - "`--crate-name` and `#[crate_name]` are \ - required to match, but `{}` != `{}`", - s, name - ); - sess.span_err(attr.span, &msg); - } + if let Some(ref s) = sess.opts.crate_name { + if let Some((attr, name)) = attr_crate_name { + if name.as_str() != *s { + let msg = format!( + "`--crate-name` and `#[crate_name]` are \ + required to match, but `{}` != `{}`", + s, name + ); + sess.span_err(attr.span, &msg); } - return validate(s.clone(), None); } + return validate(s.clone(), None); } if let Some((attr, s)) = attr_crate_name { return validate(s.to_string(), Some(attr.span)); } + if let Input::File(ref path) = *input { if let Some(s) = path.file_stem().and_then(|s| s.to_str()) { if s.starts_with('-') { @@ -85,9 +84,7 @@ pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: `{}` has a leading hyphen", s ); - if let Some(sess) = sess { - sess.err(&msg); - } + sess.err(&msg); } else { return validate(s.replace("-", "_"), None); } @@ -97,14 +94,13 @@ pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: "rust_out".to_string() } -pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option) { +pub fn validate_crate_name(sess: &Session, s: &str, sp: Option) { let mut err_count = 0; { let mut say = |s: &str| { - match (sp, sess) { - (_, None) => panic!("{}", s), - (Some(sp), Some(sess)) => sess.span_err(sp, s), - (None, Some(sess)) => sess.err(s), + match sp { + Some(sp) => sess.span_err(sp, s), + None => sess.err(s), } err_count += 1; }; @@ -123,7 +119,7 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option) { } if err_count > 0 { - sess.unwrap().abort_if_errors(); + sess.abort_if_errors(); } }