Skip to content

Commit 9f45b24

Browse files
committed
fix clippy
1 parent 8ea87bd commit 9f45b24

File tree

12 files changed

+293
-272
lines changed

12 files changed

+293
-272
lines changed

src/tools/clippy/clippy_lints/src/doc.rs

+52-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3-
use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty};
3+
use clippy_utils::{
4+
is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty,
5+
};
46
use if_chain::if_chain;
57
use itertools::Itertools;
68
use rustc_ast::ast::{Async, AttrKind, Attribute, FnKind, FnRetTy, ItemKind};
@@ -195,10 +197,7 @@ pub struct DocMarkdown {
195197

196198
impl DocMarkdown {
197199
pub fn new(valid_idents: FxHashSet<String>) -> Self {
198-
Self {
199-
valid_idents,
200-
in_trait_impl: false,
201-
}
200+
Self { valid_idents, in_trait_impl: false }
202201
}
203202
}
204203

@@ -217,11 +216,13 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
217216
let headers = check_attrs(cx, &self.valid_idents, attrs);
218217
match item.kind {
219218
hir::ItemKind::Fn(ref sig, _, body_id) => {
220-
if !(is_entrypoint_fn(cx, item.def_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) {
219+
if !(is_entrypoint_fn(cx, item.def_id.to_def_id())
220+
|| in_external_macro(cx.tcx.sess, item.span))
221+
{
221222
let body = cx.tcx.hir().body(body_id);
222223
let mut fpu = FindPanicUnwrap {
223224
cx,
224-
typeck_results: cx.tcx.typeck(item.def_id),
225+
typeck_results: cx.tcx.typeck(item.def_id.def_id),
225226
panic_span: None,
226227
};
227228
fpu.visit_expr(&body.value);
@@ -235,11 +236,11 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
235236
fpu.panic_span,
236237
);
237238
}
238-
},
239+
}
239240
hir::ItemKind::Impl(ref impl_) => {
240241
self.in_trait_impl = impl_.of_trait.is_some();
241-
},
242-
_ => {},
242+
}
243+
_ => {}
243244
}
244245
}
245246

@@ -269,7 +270,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
269270
let body = cx.tcx.hir().body(body_id);
270271
let mut fpu = FindPanicUnwrap {
271272
cx,
272-
typeck_results: cx.tcx.typeck(item.def_id),
273+
typeck_results: cx.tcx.typeck(item.def_id.def_id),
273274
panic_span: None,
274275
};
275276
fpu.visit_expr(&body.value);
@@ -299,12 +300,7 @@ fn lint_for_missing_headers<'tcx>(
299300
return; // Private functions do not require doc comments
300301
}
301302
if !headers.safety && sig.header.unsafety == hir::Unsafety::Unsafe {
302-
span_lint(
303-
cx,
304-
MISSING_SAFETY_DOC,
305-
span,
306-
"unsafe function's docs miss `# Safety` section",
307-
);
303+
span_lint(cx, MISSING_SAFETY_DOC, span, "unsafe function's docs miss `# Safety` section");
308304
}
309305
if !headers.panics && panic_span.is_some() {
310306
span_lint_and_note(
@@ -357,7 +353,11 @@ fn lint_for_missing_headers<'tcx>(
357353
/// the spans but this function is inspired from the later.
358354
#[allow(clippy::cast_possible_truncation)]
359355
#[must_use]
360-
pub fn strip_doc_comment_decoration(doc: &str, comment_kind: CommentKind, span: Span) -> (String, Vec<(usize, Span)>) {
356+
pub fn strip_doc_comment_decoration(
357+
doc: &str,
358+
comment_kind: CommentKind,
359+
span: Span,
360+
) -> (String, Vec<(usize, Span)>) {
361361
// one-line comments lose their prefix
362362
if comment_kind == CommentKind::Line {
363363
let mut doc = doc.to_owned();
@@ -405,23 +405,24 @@ struct DocHeaders {
405405
panics: bool,
406406
}
407407

408-
fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &'a [Attribute]) -> DocHeaders {
408+
fn check_attrs<'a>(
409+
cx: &LateContext<'_>,
410+
valid_idents: &FxHashSet<String>,
411+
attrs: &'a [Attribute],
412+
) -> DocHeaders {
409413
let mut doc = String::new();
410414
let mut spans = vec![];
411415

412416
for attr in attrs {
413417
if let AttrKind::DocComment(comment_kind, comment) = attr.kind {
414-
let (comment, current_spans) = strip_doc_comment_decoration(&comment.as_str(), comment_kind, attr.span);
418+
let (comment, current_spans) =
419+
strip_doc_comment_decoration(&comment.as_str(), comment_kind, attr.span);
415420
spans.extend_from_slice(&current_spans);
416421
doc.push_str(&comment);
417422
} else if attr.has_name(sym::doc) {
418423
// ignore mix of sugared and non-sugared doc
419424
// don't trigger the safety or errors check
420-
return DocHeaders {
421-
safety: true,
422-
errors: true,
423-
panics: true,
424-
};
425+
return DocHeaders { safety: true, errors: true, panics: true };
425426
}
426427
}
427428

@@ -433,11 +434,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
433434
}
434435

435436
if doc.is_empty() {
436-
return DocHeaders {
437-
safety: false,
438-
errors: false,
439-
panics: false,
440-
};
437+
return DocHeaders { safety: false, errors: false, panics: false };
441438
}
442439

443440
let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();
@@ -453,7 +450,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
453450
let mut previous = previous.to_string();
454451
previous.push_str(&current);
455452
Ok((Text(previous.into()), previous_range))
456-
},
453+
}
457454
(previous, current) => Err(((previous, previous_range), (current, current_range))),
458455
}
459456
});
@@ -475,11 +472,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
475472
};
476473
use pulldown_cmark::Tag::{CodeBlock, Heading, Link};
477474

478-
let mut headers = DocHeaders {
479-
safety: false,
480-
errors: false,
481-
panics: false,
482-
};
475+
let mut headers = DocHeaders { safety: false, errors: false, panics: false };
483476
let mut in_code = false;
484477
let mut in_link = None;
485478
let mut in_heading = false;
@@ -503,11 +496,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
503496
}
504497
}
505498
}
506-
},
499+
}
507500
End(CodeBlock(_)) => {
508501
in_code = false;
509502
is_rust = false;
510-
},
503+
}
511504
Start(Link(_, url, _)) => in_link = Some(url),
512505
End(Link(..)) => in_link = None,
513506
Start(Heading(_)) => in_heading = true,
@@ -541,7 +534,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
541534

542535
check_text(cx, valid_idents, &text, span);
543536
}
544-
},
537+
}
545538
}
546539
}
547540
headers
@@ -554,19 +547,21 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
554547
let filename = FileName::anon_source_code(code);
555548

556549
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
557-
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
550+
let emitter =
551+
EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
558552
let handler = Handler::with_emitter(false, None, box emitter);
559553
let sess = ParseSess::with_span_handler(handler, sm);
560554

561-
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code.into()) {
562-
Ok(p) => p,
563-
Err(errs) => {
564-
for mut err in errs {
565-
err.cancel();
555+
let mut parser =
556+
match maybe_new_parser_from_source_str(&sess, filename, code.into()) {
557+
Ok(p) => p,
558+
Err(errs) => {
559+
for mut err in errs {
560+
err.cancel();
561+
}
562+
return false;
566563
}
567-
return false;
568-
},
569-
};
564+
};
570565

571566
let mut relevant_main_found = false;
572567
loop {
@@ -578,7 +573,9 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
578573
| ItemKind::ExternCrate(..)
579574
| ItemKind::ForeignMod(..) => return false,
580575
// We found a main function ...
581-
ItemKind::Fn(box FnKind(_, sig, _, Some(block))) if item.ident.name == sym::main => {
576+
ItemKind::Fn(box FnKind(_, sig, _, Some(block)))
577+
if item.ident.name == sym::main =>
578+
{
582579
let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
583580
let returns_nothing = match &sig.decl.output {
584581
FnRetTy::Default(..) => true,
@@ -593,16 +590,16 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
593590
// This main function should not be linted, we're done
594591
return false;
595592
}
596-
},
593+
}
597594
// Another function was found; this case is ignored too
598595
ItemKind::Fn(..) => return false,
599-
_ => {},
596+
_ => {}
600597
},
601598
Ok(None) => break,
602599
Err(mut e) => {
603600
e.cancel();
604601
return false;
605-
},
602+
}
606603
}
607604
}
608605

@@ -722,7 +719,9 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
722719
}
723720

724721
// check for `assert_eq` or `assert_ne`
725-
if is_expn_of(expr.span, "assert_eq").is_some() || is_expn_of(expr.span, "assert_ne").is_some() {
722+
if is_expn_of(expr.span, "assert_eq").is_some()
723+
|| is_expn_of(expr.span, "assert_ne").is_some()
724+
{
726725
self.panic_span = Some(expr.span);
727726
}
728727

src/tools/clippy/clippy_lints/src/fallible_impl_from.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom {
6565
}
6666
}
6767

68-
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[hir::ImplItemRef<'_>]) {
68+
fn lint_impl_body<'tcx>(
69+
cx: &LateContext<'tcx>,
70+
impl_span: Span,
71+
impl_items: &[hir::ImplItemRef<'_>],
72+
) {
6973
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7074
use rustc_hir::{Expr, ExprKind, ImplItemKind, QPath};
7175

@@ -120,7 +124,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[h
120124
let body = cx.tcx.hir().body(body_id);
121125
let mut fpu = FindPanicUnwrap {
122126
lcx: cx,
123-
typeck_results: cx.tcx.typeck(impl_item.id.def_id),
127+
typeck_results: cx.tcx.typeck(impl_item.id.def_id.def_id),
124128
result: Vec::new(),
125129
};
126130
fpu.visit_expr(&body.value);

src/tools/clippy/clippy_lints/src/functions/must_use.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
2727
if let Some(attr) = attr {
2828
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
2929
return;
30-
} else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
30+
} else if is_public
31+
&& !is_proc_macro(cx.sess(), attrs)
32+
&& !attrs.iter().any(|a| a.has_name(sym::no_mangle))
33+
{
3134
check_must_use_candidate(
3235
cx,
3336
sig.decl,
@@ -49,7 +52,10 @@ pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<
4952
let attr = must_use_attr(attrs);
5053
if let Some(attr) = attr {
5154
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
52-
} else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.hir_id()).is_none() {
55+
} else if is_public
56+
&& !is_proc_macro(cx.sess(), attrs)
57+
&& trait_ref_of_method(cx, item.hir_id()).is_none()
58+
{
5359
check_must_use_candidate(
5460
cx,
5561
sig.decl,
@@ -178,28 +184,33 @@ fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet)
178184
return false; // ignore `_` patterns
179185
}
180186
if cx.tcx.has_typeck_results(pat.hir_id.owner.to_def_id()) {
181-
is_mutable_ty(cx, cx.tcx.typeck(pat.hir_id.owner).pat_ty(pat), pat.span, tys)
187+
is_mutable_ty(cx, cx.tcx.typeck(pat.hir_id.owner.def_id).pat_ty(pat), pat.span, tys)
182188
} else {
183189
false
184190
}
185191
}
186192

187193
static KNOWN_WRAPPER_TYS: &[&[&str]] = &[&["alloc", "rc", "Rc"], &["std", "sync", "Arc"]];
188194

189-
fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut DefIdSet) -> bool {
195+
fn is_mutable_ty<'tcx>(
196+
cx: &LateContext<'tcx>,
197+
ty: Ty<'tcx>,
198+
span: Span,
199+
tys: &mut DefIdSet,
200+
) -> bool {
190201
match *ty.kind() {
191202
// primitive types are never mutable
192203
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
193204
ty::Adt(adt, substs) => {
194205
tys.insert(adt.did) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
195206
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did, path))
196207
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
197-
},
208+
}
198209
ty::Tuple(substs) => substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)),
199210
ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, span, tys),
200211
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => {
201212
mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys)
202-
},
213+
}
203214
// calling something constitutes a side effect, so return true on all callables
204215
// also never calls need not be used, so return true for them, too
205216
_ => true,
@@ -227,7 +238,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
227238
if self.cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
228239
&& is_mutable_ty(
229240
self.cx,
230-
self.cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg),
241+
self.cx.tcx.typeck(arg.hir_id.owner.def_id).expr_ty(arg),
231242
arg.span,
232243
&mut tys,
233244
)
@@ -238,11 +249,13 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
238249
}
239250
tys.clear();
240251
}
241-
},
242-
Assign(target, ..) | AssignOp(_, target, _) | AddrOf(_, hir::Mutability::Mut, target) => {
252+
}
253+
Assign(target, ..)
254+
| AssignOp(_, target, _)
255+
| AddrOf(_, hir::Mutability::Mut, target) => {
243256
self.mutates_static |= is_mutated_static(target)
244-
},
245-
_ => {},
257+
}
258+
_ => {}
246259
}
247260
}
248261

@@ -263,10 +276,7 @@ fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
263276
}
264277

265278
fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
266-
let mut v = StaticMutVisitor {
267-
cx,
268-
mutates_static: false,
269-
};
279+
let mut v = StaticMutVisitor { cx, mutates_static: false };
270280
intravisit::walk_expr(&mut v, &body.value);
271281
v.mutates_static
272282
}

0 commit comments

Comments
 (0)