Skip to content

Commit 0b2436f

Browse files
committed
Move more into decorate functions.
1 parent aa3c458 commit 0b2436f

File tree

9 files changed

+58
-51
lines changed

9 files changed

+58
-51
lines changed

src/librustc_lint/builtin.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,15 @@ impl EarlyLintPass for AnonymousParameters {
645645
match arg.pat.kind {
646646
ast::PatKind::Ident(_, ident, None) => {
647647
if ident.name == kw::Invalid {
648-
let ty_snip = cx.sess.source_map().span_to_snippet(arg.ty.span);
648+
cx.struct_span_lint(ANONYMOUS_PARAMETERS, arg.pat.span, |lint| {
649+
let ty_snip = cx.sess.source_map().span_to_snippet(arg.ty.span);
649650

650-
let (ty_snip, appl) = if let Ok(snip) = ty_snip {
651-
(snip, Applicability::MachineApplicable)
652-
} else {
653-
("<type>".to_owned(), Applicability::HasPlaceholders)
654-
};
651+
let (ty_snip, appl) = if let Ok(snip) = ty_snip {
652+
(snip, Applicability::MachineApplicable)
653+
} else {
654+
("<type>".to_owned(), Applicability::HasPlaceholders)
655+
};
655656

656-
cx.struct_span_lint(ANONYMOUS_PARAMETERS, arg.pat.span, |lint| {
657657
lint.build(
658658
"anonymous parameters are deprecated and will be \
659659
removed in the next edition.",
@@ -869,8 +869,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
869869
if attr::contains_name(&it.attrs, sym::no_mangle) {
870870
// Const items do not refer to a particular location in memory, and therefore
871871
// don't have anything to attach a symbol to
872-
let msg = "const items should never be `#[no_mangle]`";
873872
cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, |lint| {
873+
let msg = "const items should never be `#[no_mangle]`";
874874
let mut err = lint.build(msg);
875875

876876
// account for "pub const" (#45562)
@@ -910,11 +910,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes {
910910
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) {
911911
use rustc_target::spec::abi::Abi::RustIntrinsic;
912912

913-
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
914-
consider instead using an UnsafeCell";
915913
match get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (&ty1.kind, &ty2.kind)) {
916914
Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) => {
917915
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
916+
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
917+
consider instead using an UnsafeCell";
918918
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| {
919919
lint.build(msg).emit()
920920
});
@@ -1335,12 +1335,12 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
13351335
let suggestion = "use `..=` for an inclusive range";
13361336
if parenthesise {
13371337
self.node_id = Some(pat.id);
1338-
let end = expr_to_string(&end);
1339-
let replace = match start {
1340-
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1341-
None => format!("&(..={})", end),
1342-
};
13431338
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
1339+
let end = expr_to_string(&end);
1340+
let replace = match start {
1341+
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1342+
None => format!("&(..={})", end),
1343+
};
13441344
lint.build(msg)
13451345
.span_suggestion(
13461346
pat.span,

src/librustc_lint/internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
3737
impl EarlyLintPass for DefaultHashTypes {
3838
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
3939
if let Some(replace) = self.map.get(&ident.name) {
40-
// FIXME: We can avoid a copy here. Would require us to take String instead of &str.
41-
let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
4240
cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, |lint| {
41+
// FIXME: We can avoid a copy here. Would require us to take String instead of &str.
42+
let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
4343
lint.build(&msg)
4444
.span_suggestion(
4545
ident.span,

src/librustc_lint/levels.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,19 @@ impl<'s> LintLevelsBuilder<'s> {
234234
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
235235
let (lvl, src) =
236236
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
237-
let msg = format!(
238-
"lint name `{}` is deprecated \
239-
and may not have an effect in the future. \
240-
Also `cfg_attr(cargo-clippy)` won't be necessary anymore",
241-
name
242-
);
243237
struct_lint_level(
244238
self.sess,
245239
lint,
246240
lvl,
247241
src,
248242
Some(li.span().into()),
249243
|lint| {
244+
let msg = format!(
245+
"lint name `{}` is deprecated \
246+
and may not have an effect in the future. \
247+
Also `cfg_attr(cargo-clippy)` won't be necessary anymore",
248+
name
249+
);
250250
lint.build(&msg)
251251
.span_suggestion(
252252
li.span(),
@@ -306,15 +306,14 @@ impl<'s> LintLevelsBuilder<'s> {
306306
let lint = builtin::UNKNOWN_LINTS;
307307
let (level, src) =
308308
self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
309-
let msg = format!("unknown lint: `{}`", name);
310309
struct_lint_level(
311310
self.sess,
312311
lint,
313312
level,
314313
src,
315314
Some(li.span().into()),
316315
|lint| {
317-
let mut db = lint.build(&msg);
316+
let mut db = lint.build(&format!("unknown lint: `{}`", name));
318317
if let Some(suggestion) = suggestion {
319318
db.span_suggestion(
320319
li.span(),

src/librustc_lint/unused.rs

+4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
202202
}
203203

204204
// Returns whether an error has been emitted (and thus another does not need to be later).
205+
// FIXME: Args desc_{pre,post}_path could be made lazy by taking Fn() -> &str, but this
206+
// would make calling it a big awkward. Could also take String (so args are moved), but
207+
// this would still require a copy into the format string, which would only be executed
208+
// when needed.
205209
fn check_must_use_def(
206210
cx: &LateContext<'_, '_>,
207211
def_id: DefId,

src/librustc_mir/borrow_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ fn do_mir_borrowck<'a, 'tcx>(
378378
continue;
379379
}
380380

381-
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
382381
tcx.struct_span_lint_hir(UNUSED_MUT, lint_root, span, |lint| {
382+
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
383383
lint.build("variable does not need to be mutable")
384384
.span_suggestion_short(
385385
mut_span,

src/librustc_mir/transform/check_unsafety.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -516,18 +516,18 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: DefId) {
516516
.as_local_hir_id(def_id)
517517
.unwrap_or_else(|| bug!("checking unsafety for non-local def id {:?}", def_id));
518518

519-
// FIXME: when we make this a hard error, this should have its
520-
// own error code.
521-
let message = if tcx.generics_of(def_id).own_requires_monomorphization() {
522-
"`#[derive]` can't be used on a `#[repr(packed)]` struct with \
523-
type or const parameters (error E0133)"
524-
.to_string()
525-
} else {
526-
"`#[derive]` can't be used on a `#[repr(packed)]` struct that \
527-
does not derive Copy (error E0133)"
528-
.to_string()
529-
};
530519
tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| {
520+
// FIXME: when we make this a hard error, this should have its
521+
// own error code.
522+
let message = if tcx.generics_of(def_id).own_requires_monomorphization() {
523+
"`#[derive]` can't be used on a `#[repr(packed)]` struct with \
524+
type or const parameters (error E0133)"
525+
.to_string()
526+
} else {
527+
"`#[derive]` can't be used on a `#[repr(packed)]` struct that \
528+
does not derive Copy (error E0133)"
529+
.to_string()
530+
};
531531
lint.build(&message).emit()
532532
});
533533
}
@@ -560,8 +560,8 @@ fn is_enclosed(
560560

561561
fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id: hir::HirId) {
562562
let span = tcx.sess.source_map().def_span(tcx.hir().span(id));
563-
let msg = "unnecessary `unsafe` block";
564563
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
564+
let msg = "unnecessary `unsafe` block";
565565
let mut db = lint.build(msg);
566566
db.span_label(span, msg);
567567
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {

src/librustc_mir/transform/const_prop.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
556556
let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
557557
if r_bits.map_or(false, |b| b >= left_bits as u128) {
558558
let lint_root = self.lint_root(source_info)?;
559-
let dir = if op == BinOp::Shr { "right" } else { "left" };
560559
self.tcx.struct_span_lint_hir(
561560
::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
562561
lint_root,
563562
source_info.span,
564-
|lint| lint.build(&format!("attempt to shift {} with overflow", dir)).emit(),
563+
|lint| {
564+
let dir = if op == BinOp::Shr { "right" } else { "left" };
565+
lint.build(&format!("attempt to shift {} with overflow", dir)).emit()
566+
},
565567
);
566568
return None;
567569
}

src/librustc_mir_build/hair/pattern/check_match.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
286286
variant.ident == ident && variant.ctor_kind == CtorKind::Const
287287
})
288288
{
289-
let ty_path = cx.tcx.def_path_str(edef.did);
290289
cx.tcx.struct_span_lint_hir(
291290
BINDINGS_WITH_VARIANT_NAME,
292291
p.hir_id,
293292
p.span,
294293
|lint| {
294+
let ty_path = cx.tcx.def_path_str(edef.did);
295295
lint.build(&format!(
296296
"pattern binding `{}` is named the same as one \
297297
of the variants of the type `{}`",
@@ -338,12 +338,14 @@ fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option<
338338
}
339339

340340
fn irrefutable_let_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, source: hir::MatchSource) {
341-
let msg = match source {
342-
hir::MatchSource::IfLetDesugar { .. } => "irrefutable if-let pattern",
343-
hir::MatchSource::WhileLetDesugar => "irrefutable while-let pattern",
344-
_ => bug!(),
345-
};
346-
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| lint.build(msg).emit());
341+
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| {
342+
let msg = match source {
343+
hir::MatchSource::IfLetDesugar { .. } => "irrefutable if-let pattern",
344+
hir::MatchSource::WhileLetDesugar => "irrefutable while-let pattern",
345+
_ => bug!(),
346+
};
347+
lint.build(msg).emit()
348+
});
347349
}
348350

349351
/// Check for unreachable patterns.

src/librustc_privacy/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1805,12 +1805,12 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
18051805

18061806
let (vis, vis_span, vis_descr) = def_id_visibility(self.tcx, def_id);
18071807
if !vis.is_at_least(self.required_visibility, self.tcx) {
1808-
let msg = format!("{} {} `{}` in public interface", vis_descr, kind, descr);
1808+
let make_msg = || format!("{} {} `{}` in public interface", vis_descr, kind, descr);
18091809
if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
18101810
let mut err = if kind == "trait" {
1811-
struct_span_err!(self.tcx.sess, self.span, E0445, "{}", msg)
1811+
struct_span_err!(self.tcx.sess, self.span, E0445, "{}", make_msg())
18121812
} else {
1813-
struct_span_err!(self.tcx.sess, self.span, E0446, "{}", msg)
1813+
struct_span_err!(self.tcx.sess, self.span, E0446, "{}", make_msg())
18141814
};
18151815
err.span_label(self.span, format!("can't leak {} {}", vis_descr, kind));
18161816
err.span_label(vis_span, format!("`{}` declared as {}", descr, vis_descr));
@@ -1821,7 +1821,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
18211821
lint::builtin::PRIVATE_IN_PUBLIC,
18221822
hir_id,
18231823
self.span,
1824-
|lint| lint.build(&format!("{} (error {})", msg, err_code)).emit(),
1824+
|lint| lint.build(&format!("{} (error {})", make_msg(), err_code)).emit(),
18251825
);
18261826
}
18271827
}

0 commit comments

Comments
 (0)