diff options
| author | jumbatm <jumbatm@gmail.com> | 2020-02-06 01:27:46 +1000 |
|---|---|---|
| committer | jumbatm <jumbatm@gmail.com> | 2020-02-11 19:50:26 +1000 |
| commit | 0b2436f3a466ff077861665197cf35059c811f48 (patch) | |
| tree | e9b071c516b130a3ccaffb32f9f146e5121e2b82 | |
| parent | aa3c458c067b399e1f115281f77b7f33513d5920 (diff) | |
| download | rust-0b2436f3a466ff077861665197cf35059c811f48.tar.gz rust-0b2436f3a466ff077861665197cf35059c811f48.zip | |
Move more into decorate functions.
| -rw-r--r-- | src/librustc_lint/builtin.rs | 30 | ||||
| -rw-r--r-- | src/librustc_lint/internal.rs | 4 | ||||
| -rw-r--r-- | src/librustc_lint/levels.rs | 15 | ||||
| -rw-r--r-- | src/librustc_lint/unused.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc_mir/transform/check_unsafety.rs | 24 | ||||
| -rw-r--r-- | src/librustc_mir/transform/const_prop.rs | 6 | ||||
| -rw-r--r-- | src/librustc_mir_build/hair/pattern/check_match.rs | 16 | ||||
| -rw-r--r-- | src/librustc_privacy/lib.rs | 8 |
9 files changed, 58 insertions, 51 deletions
diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 23bbcb40c94..9cc3e39726b 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -645,15 +645,15 @@ impl EarlyLintPass for AnonymousParameters { match arg.pat.kind { ast::PatKind::Ident(_, ident, None) => { if ident.name == kw::Invalid { - let ty_snip = cx.sess.source_map().span_to_snippet(arg.ty.span); + cx.struct_span_lint(ANONYMOUS_PARAMETERS, arg.pat.span, |lint| { + let ty_snip = cx.sess.source_map().span_to_snippet(arg.ty.span); - let (ty_snip, appl) = if let Ok(snip) = ty_snip { - (snip, Applicability::MachineApplicable) - } else { - ("<type>".to_owned(), Applicability::HasPlaceholders) - }; + let (ty_snip, appl) = if let Ok(snip) = ty_snip { + (snip, Applicability::MachineApplicable) + } else { + ("<type>".to_owned(), Applicability::HasPlaceholders) + }; - cx.struct_span_lint(ANONYMOUS_PARAMETERS, arg.pat.span, |lint| { lint.build( "anonymous parameters are deprecated and will be \ removed in the next edition.", @@ -869,8 +869,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { if attr::contains_name(&it.attrs, sym::no_mangle) { // Const items do not refer to a particular location in memory, and therefore // don't have anything to attach a symbol to - let msg = "const items should never be `#[no_mangle]`"; cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, |lint| { + let msg = "const items should never be `#[no_mangle]`"; let mut err = lint.build(msg); // account for "pub const" (#45562) @@ -910,11 +910,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes { fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) { use rustc_target::spec::abi::Abi::RustIntrinsic; - let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \ - consider instead using an UnsafeCell"; match get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (&ty1.kind, &ty2.kind)) { Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) => { if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not { + let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \ + consider instead using an UnsafeCell"; cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| { lint.build(msg).emit() }); @@ -1335,12 +1335,12 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns { let suggestion = "use `..=` for an inclusive range"; if parenthesise { self.node_id = Some(pat.id); - let end = expr_to_string(&end); - let replace = match start { - Some(start) => format!("&({}..={})", expr_to_string(&start), end), - None => format!("&(..={})", end), - }; cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| { + let end = expr_to_string(&end); + let replace = match start { + Some(start) => format!("&({}..={})", expr_to_string(&start), end), + None => format!("&(..={})", end), + }; lint.build(msg) .span_suggestion( pat.span, diff --git a/src/librustc_lint/internal.rs b/src/librustc_lint/internal.rs index 79a06e81410..78752619112 100644 --- a/src/librustc_lint/internal.rs +++ b/src/librustc_lint/internal.rs @@ -37,9 +37,9 @@ impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]); impl EarlyLintPass for DefaultHashTypes { fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) { if let Some(replace) = self.map.get(&ident.name) { - // FIXME: We can avoid a copy here. Would require us to take String instead of &str. - let msg = format!("Prefer {} over {}, it has better performance", replace, ident); cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, |lint| { + // FIXME: We can avoid a copy here. Would require us to take String instead of &str. + let msg = format!("Prefer {} over {}, it has better performance", replace, ident); lint.build(&msg) .span_suggestion( ident.span, diff --git a/src/librustc_lint/levels.rs b/src/librustc_lint/levels.rs index b58acac316e..da449ed42fd 100644 --- a/src/librustc_lint/levels.rs +++ b/src/librustc_lint/levels.rs @@ -234,12 +234,6 @@ impl<'s> LintLevelsBuilder<'s> { let lint = builtin::RENAMED_AND_REMOVED_LINTS; let (lvl, src) = self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess); - let msg = format!( - "lint name `{}` is deprecated \ - and may not have an effect in the future. \ - Also `cfg_attr(cargo-clippy)` won't be necessary anymore", - name - ); struct_lint_level( self.sess, lint, @@ -247,6 +241,12 @@ impl<'s> LintLevelsBuilder<'s> { src, Some(li.span().into()), |lint| { + let msg = format!( + "lint name `{}` is deprecated \ + and may not have an effect in the future. \ + Also `cfg_attr(cargo-clippy)` won't be necessary anymore", + name + ); lint.build(&msg) .span_suggestion( li.span(), @@ -306,7 +306,6 @@ impl<'s> LintLevelsBuilder<'s> { let lint = builtin::UNKNOWN_LINTS; let (level, src) = self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess); - let msg = format!("unknown lint: `{}`", name); struct_lint_level( self.sess, lint, @@ -314,7 +313,7 @@ impl<'s> LintLevelsBuilder<'s> { src, Some(li.span().into()), |lint| { - let mut db = lint.build(&msg); + let mut db = lint.build(&format!("unknown lint: `{}`", name)); if let Some(suggestion) = suggestion { db.span_suggestion( li.span(), diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 9cf5ff42c57..480df99a01e 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -202,6 +202,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults { } // Returns whether an error has been emitted (and thus another does not need to be later). + // FIXME: Args desc_{pre,post}_path could be made lazy by taking Fn() -> &str, but this + // would make calling it a big awkward. Could also take String (so args are moved), but + // this would still require a copy into the format string, which would only be executed + // when needed. fn check_must_use_def( cx: &LateContext<'_, '_>, def_id: DefId, diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 4dae7fbe941..82a9c631b72 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -378,8 +378,8 @@ fn do_mir_borrowck<'a, 'tcx>( continue; } - let mut_span = tcx.sess.source_map().span_until_non_whitespace(span); tcx.struct_span_lint_hir(UNUSED_MUT, lint_root, span, |lint| { + let mut_span = tcx.sess.source_map().span_until_non_whitespace(span); lint.build("variable does not need to be mutable") .span_suggestion_short( mut_span, diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 08ef60aa58e..6e80338c975 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -516,18 +516,18 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: DefId) { .as_local_hir_id(def_id) .unwrap_or_else(|| bug!("checking unsafety for non-local def id {:?}", def_id)); - // FIXME: when we make this a hard error, this should have its - // own error code. - let message = if tcx.generics_of(def_id).own_requires_monomorphization() { - "`#[derive]` can't be used on a `#[repr(packed)]` struct with \ - type or const parameters (error E0133)" - .to_string() - } else { - "`#[derive]` can't be used on a `#[repr(packed)]` struct that \ - does not derive Copy (error E0133)" - .to_string() - }; tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| { + // FIXME: when we make this a hard error, this should have its + // own error code. + let message = if tcx.generics_of(def_id).own_requires_monomorphization() { + "`#[derive]` can't be used on a `#[repr(packed)]` struct with \ + type or const parameters (error E0133)" + .to_string() + } else { + "`#[derive]` can't be used on a `#[repr(packed)]` struct that \ + does not derive Copy (error E0133)" + .to_string() + }; lint.build(&message).emit() }); } @@ -560,8 +560,8 @@ fn is_enclosed( fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id: hir::HirId) { let span = tcx.sess.source_map().def_span(tcx.hir().span(id)); - let msg = "unnecessary `unsafe` block"; tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { + let msg = "unnecessary `unsafe` block"; let mut db = lint.build(msg); db.span_label(span, msg); if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) { diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 9a2e2c56f14..14c0db2def2 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -556,12 +556,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size)); if r_bits.map_or(false, |b| b >= left_bits as u128) { let lint_root = self.lint_root(source_info)?; - let dir = if op == BinOp::Shr { "right" } else { "left" }; self.tcx.struct_span_lint_hir( ::rustc::lint::builtin::EXCEEDING_BITSHIFTS, lint_root, source_info.span, - |lint| lint.build(&format!("attempt to shift {} with overflow", dir)).emit(), + |lint| { + let dir = if op == BinOp::Shr { "right" } else { "left" }; + lint.build(&format!("attempt to shift {} with overflow", dir)).emit() + }, ); return None; } diff --git a/src/librustc_mir_build/hair/pattern/check_match.rs b/src/librustc_mir_build/hair/pattern/check_match.rs index acb9829ce5f..651f2f70d9b 100644 --- a/src/librustc_mir_build/hair/pattern/check_match.rs +++ b/src/librustc_mir_build/hair/pattern/check_match.rs @@ -286,12 +286,12 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa variant.ident == ident && variant.ctor_kind == CtorKind::Const }) { - let ty_path = cx.tcx.def_path_str(edef.did); cx.tcx.struct_span_lint_hir( BINDINGS_WITH_VARIANT_NAME, p.hir_id, p.span, |lint| { + let ty_path = cx.tcx.def_path_str(edef.did); lint.build(&format!( "pattern binding `{}` is named the same as one \ of the variants of the type `{}`", @@ -338,12 +338,14 @@ fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option< } fn irrefutable_let_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, source: hir::MatchSource) { - let msg = match source { - hir::MatchSource::IfLetDesugar { .. } => "irrefutable if-let pattern", - hir::MatchSource::WhileLetDesugar => "irrefutable while-let pattern", - _ => bug!(), - }; - tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| lint.build(msg).emit()); + tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| { + let msg = match source { + hir::MatchSource::IfLetDesugar { .. } => "irrefutable if-let pattern", + hir::MatchSource::WhileLetDesugar => "irrefutable while-let pattern", + _ => bug!(), + }; + lint.build(msg).emit() + }); } /// Check for unreachable patterns. diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 7f51bc25642..ef1e99c5a64 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -1805,12 +1805,12 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> { let (vis, vis_span, vis_descr) = def_id_visibility(self.tcx, def_id); if !vis.is_at_least(self.required_visibility, self.tcx) { - let msg = format!("{} {} `{}` in public interface", vis_descr, kind, descr); + let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr); if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty { let mut err = if kind == "trait" { - struct_span_err!(self.tcx.sess, self.span, E0445, "{}", msg) + struct_span_err!(self.tcx.sess, self.span, E0445, "{}", make_msg()) } else { - struct_span_err!(self.tcx.sess, self.span, E0446, "{}", msg) + struct_span_err!(self.tcx.sess, self.span, E0446, "{}", make_msg()) }; err.span_label(self.span, format!("can't leak {} {}", vis_descr, kind)); err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr)); @@ -1821,7 +1821,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> { lint::builtin::PRIVATE_IN_PUBLIC, hir_id, self.span, - |lint| lint.build(&format!("{} (error {})", msg, err_code)).emit(), + |lint| lint.build(&format!("{} (error {})", make_msg(), err_code)).emit(), ); } } |
