Skip to content

Commit

Permalink
Run RustFmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jumbatm committed Feb 3, 2020
1 parent eaf8fa7 commit bc5bf23
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 77 deletions.
106 changes: 64 additions & 42 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc::hir::map::Map;
use rustc::lint::LintDiagnosticBuilder;
use rustc::traits::misc::can_type_implement_copy;
use rustc::ty::{self, layout::VariantIdx, Ty, TyCtxt};
use rustc_ast_pretty::pprust::{self, expr_to_string};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc::lint::LintDiagnosticBuilder;
use rustc_feature::Stability;
use rustc_feature::{deprecated_attributes, AttributeGate, AttributeTemplate, AttributeType};
use rustc_hir as hir;
Expand Down Expand Up @@ -107,7 +107,9 @@ impl BoxPointers {
fn check_heap_type(&self, cx: &LateContext<'_, '_>, span: Span, ty: Ty<'_>) {
for leaf_ty in ty.walk() {
if leaf_ty.is_box() {
cx.struct_span_lint(BOX_POINTERS, span, |lint| lint.build(&format!("type uses owned (Box type) pointers: {}", ty)).emit());
cx.struct_span_lint(BOX_POINTERS, span, |lint| {
lint.build(&format!("type uses owned (Box type) pointers: {}", ty)).emit()
});
}
}
}
Expand Down Expand Up @@ -214,7 +216,12 @@ declare_lint! {
declare_lint_pass!(UnsafeCode => [UNSAFE_CODE]);

impl UnsafeCode {
fn report_unsafe(&self, cx: &EarlyContext<'_>, span: Span, decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>)) {
fn report_unsafe(
&self,
cx: &EarlyContext<'_>,
span: Span,
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
) {
// This comes from a macro that has `#[allow_internal_unsafe]`.
if span.allows_unsafe() {
return;
Expand All @@ -227,33 +234,40 @@ impl UnsafeCode {
impl EarlyLintPass for UnsafeCode {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
if attr.check_name(sym::allow_internal_unsafe) {
self.report_unsafe(
cx,
attr.span,
|lint| lint.build("`allow_internal_unsafe` allows defining \
self.report_unsafe(cx, attr.span, |lint| {
lint.build(
"`allow_internal_unsafe` allows defining \
macros using unsafe without triggering \
the `unsafe_code` lint at their call site").emit(),
);
the `unsafe_code` lint at their call site",
)
.emit()
});
}
}

fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
if let ast::ExprKind::Block(ref blk, _) = e.kind {
// Don't warn about generated blocks; that'll just pollute the output.
if blk.rules == ast::BlockCheckMode::Unsafe(ast::UserProvided) {
self.report_unsafe(cx, blk.span, |lint| lint.build("usage of an `unsafe` block").emit());
self.report_unsafe(cx, blk.span, |lint| {
lint.build("usage of an `unsafe` block").emit()
});
}
}
}

fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
match it.kind {
ast::ItemKind::Trait(_, ast::Unsafety::Unsafe, ..) => {
self.report_unsafe(cx, it.span, |lint| lint.build("declaration of an `unsafe` trait").emit())
self.report_unsafe(cx, it.span, |lint| {
lint.build("declaration of an `unsafe` trait").emit()
})
}

ast::ItemKind::Impl { unsafety: ast::Unsafety::Unsafe, .. } => {
self.report_unsafe(cx, it.span, |lint| lint.build("implementation of an `unsafe` trait").emit())
self.report_unsafe(cx, it.span, |lint| {
lint.build("implementation of an `unsafe` trait").emit()
})
}

_ => return,
Expand All @@ -269,13 +283,16 @@ impl EarlyLintPass for UnsafeCode {
_: ast::NodeId,
) {
match fk {
FnKind::ItemFn(_, ast::FnHeader { unsafety: ast::Unsafety::Unsafe, .. }, ..) => {
self.report_unsafe(cx, span, |lint| lint.build("declaration of an `unsafe` function").emit())
}
FnKind::ItemFn(_, ast::FnHeader { unsafety: ast::Unsafety::Unsafe, .. }, ..) => self
.report_unsafe(cx, span, |lint| {
lint.build("declaration of an `unsafe` function").emit()
}),

FnKind::Method(_, sig, ..) => {
if sig.header.unsafety == ast::Unsafety::Unsafe {
self.report_unsafe(cx, span, |lint| lint.build("implementation of an `unsafe` method").emit())
self.report_unsafe(cx, span, |lint| {
lint.build("implementation of an `unsafe` method").emit()
})
}
}

Expand All @@ -286,7 +303,9 @@ impl EarlyLintPass for UnsafeCode {
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, item: &ast::AssocItem) {
if let ast::AssocItemKind::Fn(ref sig, None) = item.kind {
if sig.header.unsafety == ast::Unsafety::Unsafe {
self.report_unsafe(cx, item.span, |lint| lint.build("declaration of an `unsafe` method").emit())
self.report_unsafe(cx, item.span, |lint| {
lint.build("declaration of an `unsafe` method").emit()
})
}
}
}
Expand Down Expand Up @@ -372,11 +391,9 @@ impl MissingDoc {

let has_doc = attrs.iter().any(|a| has_doc(a));
if !has_doc {
cx.struct_span_lint(
MISSING_DOCS,
cx.tcx.sess.source_map().def_span(sp),
|lint| lint.build(&format!("missing documentation for {}", desc)).emit(),
);
cx.struct_span_lint(MISSING_DOCS, cx.tcx.sess.source_map().def_span(sp), |lint| {
lint.build(&format!("missing documentation for {}", desc)).emit()
});
}
}
}
Expand Down Expand Up @@ -555,12 +572,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingCopyImplementations {
return;
}
if can_type_implement_copy(cx.tcx, param_env, ty).is_ok() {
cx.struct_span_lint(
MISSING_COPY_IMPLEMENTATIONS,
item.span,
|lint| lint.build("type could implement `Copy`; consider adding `impl \
Copy`").emit(),
)
cx.struct_span_lint(MISSING_COPY_IMPLEMENTATIONS, item.span, |lint| {
lint.build(
"type could implement `Copy`; consider adding `impl \
Copy`",
)
.emit()
})
}
}
}
Expand Down Expand Up @@ -609,12 +627,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
}

if !self.impling_types.as_ref().unwrap().contains(&item.hir_id) {
cx.struct_span_lint(
MISSING_DEBUG_IMPLEMENTATIONS,
item.span,
|lint| lint.build("type does not implement `fmt::Debug`; consider adding `#[derive(Debug)]` \
or a manual implementation").emit(),
)
cx.struct_span_lint(MISSING_DEBUG_IMPLEMENTATIONS, item.span, |lint| {
lint.build(
"type does not implement `fmt::Debug`; consider adding `#[derive(Debug)]` \
or a manual implementation",
)
.emit()
})
}
}
}
Expand Down Expand Up @@ -912,7 +931,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes {
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 {
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit());
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| {
lint.build(msg).emit()
});
}
}
_ => (),
Expand Down Expand Up @@ -962,7 +983,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnstableFeatures {
if attr.check_name(sym::feature) {
if let Some(items) = attr.meta_item_list() {
for item in items {
ctx.struct_span_lint(UNSTABLE_FEATURES, item.span(), |lint| lint.build("unstable feature").emit());
ctx.struct_span_lint(UNSTABLE_FEATURES, item.span(), |lint| {
lint.build("unstable feature").emit()
});
}
}
}
Expand Down Expand Up @@ -1244,15 +1267,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
ConstEvaluatable(..) => continue,
};
if predicate.is_global() {
cx.struct_span_lint(
TRIVIAL_BOUNDS,
span,
|lint| lint.build(&format!(
cx.struct_span_lint(TRIVIAL_BOUNDS, span, |lint| {
lint.build(&format!(
"{} bound {} does not depend on any type \
or lifetime parameters",
predicate_kind_name, predicate
)).emit(),
);
))
.emit()
});
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_lint/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ struct EarlyContextAndPass<'a, T: EarlyLintPass> {
impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
fn check_id(&mut self, id: ast::NodeId) {
for early_lint in self.context.buffered.take(id) {
let rustc_session::lint::BufferedEarlyLint { span, msg, node_id: _, lint_id: _, diagnostic } = early_lint;
let rustc_session::lint::BufferedEarlyLint {
span,
msg,
node_id: _,
lint_id: _,
diagnostic,
} = early_lint;
self.context.lookup_with_diagnostics(
early_lint.lint_id.lint,
Some(span),
Expand Down
43 changes: 19 additions & 24 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ fn lint_int_literal<'a, 'tcx>(
}
}

cx.struct_span_lint(
OVERFLOWING_LITERALS,
e.span,
|lint| lint.build(&format!("literal out of range for `{}`", t.name_str())).emit(),
);
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
});
}
}

Expand Down Expand Up @@ -321,11 +319,9 @@ fn lint_uint_literal<'a, 'tcx>(
report_bin_hex_error(cx, e, attr::IntType::UnsignedInt(t), repr_str, lit_val, false);
return;
}
cx.struct_span_lint(
OVERFLOWING_LITERALS,
e.span,
|lint| lint.build(&format!("literal out of range for `{}`", t.name_str())).emit(),
);
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
});
}
}

Expand Down Expand Up @@ -355,11 +351,9 @@ fn lint_literal<'a, 'tcx>(
_ => bug!(),
};
if is_infinite == Ok(true) {
cx.struct_span_lint(
OVERFLOWING_LITERALS,
e.span,
|lint| lint.build(&format!("literal out of range for `{}`", t.name_str())).emit(),
);
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
});
}
}
_ => {}
Expand All @@ -377,11 +371,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
}
hir::ExprKind::Binary(binop, ref l, ref r) => {
if is_comparison(binop) && !check_limits(cx, binop, &l, &r) {
cx.struct_span_lint(
UNUSED_COMPARISONS,
e.span,
|lint| lint.build("comparison is useless due to type limits").emit(),
);
cx.struct_span_lint(UNUSED_COMPARISONS, e.span, |lint| {
lint.build("comparison is useless due to type limits").emit()
});
}
}
hir::ExprKind::Lit(ref lit) => lint_literal(cx, self, e, lit),
Expand Down Expand Up @@ -1058,11 +1050,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
cx.struct_span_lint(
VARIANT_SIZE_DIFFERENCES,
enum_definition.variants[largest_index].span,
|lint| lint.build(&format!(
"enum variant is more than three times \
|lint| {
lint.build(&format!(
"enum variant is more than three times \
larger ({} bytes) than the next largest",
largest
)).emit(),
largest
))
.emit()
},
);
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
};

if let Some(must_use_op) = must_use_op {
cx.struct_span_lint(
UNUSED_MUST_USE,
expr.span,
|lint| lint.build(&format!("unused {} that must be used", must_use_op)).emit(),
);
cx.struct_span_lint(UNUSED_MUST_USE, expr.span, |lint| {
lint.build(&format!("unused {} that must be used", must_use_op)).emit()
});
op_warned = true;
}

Expand Down Expand Up @@ -247,7 +245,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt<'_>) {
if let hir::StmtKind::Semi(ref expr) = s.kind {
if let hir::ExprKind::Path(_) = expr.kind {
cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| lint.build("path statement with no effect").emit());
cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| {
lint.build("path statement with no effect").emit()
});
}
}
}
Expand Down Expand Up @@ -288,7 +288,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {

if !attr::is_used(attr) {
debug!("emitting warning for: {:?}", attr);
cx.struct_span_lint(UNUSED_ATTRIBUTES, attr.span, |lint| lint.build("unused attribute").emit());
cx.struct_span_lint(UNUSED_ATTRIBUTES, attr.span, |lint| {
lint.build("unused attribute").emit()
});
// Is it a builtin attribute that must be used at the crate level?
if attr_info.map_or(false, |(_, ty, ..)| ty == &AttributeType::CrateLevel) {
cx.struct_span_lint(UNUSED_ATTRIBUTES, attr.span, |lint| {
Expand Down Expand Up @@ -629,9 +631,9 @@ impl UnusedImportBraces {
ast::UseTreeKind::Nested(_) => return,
};

cx.struct_span_lint(UNUSED_IMPORT_BRACES, item.span, |lint|
lint.build(&format!("braces around {} is unnecessary", node_name)).emit()
);
cx.struct_span_lint(UNUSED_IMPORT_BRACES, item.span, |lint| {
lint.build(&format!("braces around {} is unnecessary", node_name)).emit()
});
}
}
}
Expand Down

0 comments on commit bc5bf23

Please sign in to comment.