Skip to content

Commit 1d71d9a

Browse files
committed
Fix dogfood errors in clippy_lints
1 parent 3c502ec commit 1d71d9a

File tree

4 files changed

+77
-82
lines changed

4 files changed

+77
-82
lines changed

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,26 @@ pub(super) fn check<'tcx>(
1717
) -> bool {
1818
match (&from_ty.kind(), &to_ty.kind()) {
1919
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
20-
{
21-
span_lint_and_then(
22-
cx,
23-
TRANSMUTE_INT_TO_CHAR,
24-
e.span,
25-
&format!("transmute from a `{}` to a `char`", from_ty),
26-
|diag| {
27-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
28-
let arg = if let ty::Int(_) = from_ty.kind() {
29-
arg.as_ty(ast::UintTy::U32.name_str())
30-
} else {
31-
arg
32-
};
33-
diag.span_suggestion(
34-
e.span,
35-
"consider using",
36-
format!("std::char::from_u32({}).unwrap()", arg.to_string()),
37-
Applicability::Unspecified,
38-
);
39-
},
40-
)
41-
};
20+
span_lint_and_then(
21+
cx,
22+
TRANSMUTE_INT_TO_CHAR,
23+
e.span,
24+
&format!("transmute from a `{}` to a `char`", from_ty),
25+
|diag| {
26+
let arg = sugg::Sugg::hir(cx, &args[0], "..");
27+
let arg = if let ty::Int(_) = from_ty.kind() {
28+
arg.as_ty(ast::UintTy::U32.name_str())
29+
} else {
30+
arg
31+
};
32+
diag.span_suggestion(
33+
e.span,
34+
"consider using",
35+
format!("std::char::from_u32({}).unwrap()", arg.to_string()),
36+
Applicability::Unspecified,
37+
);
38+
},
39+
);
4240
true
4341
},
4442
_ => false,

clippy_lints/src/transmute/transmute_ptr_to_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(super) fn check<'tcx>(
1717
qpath: &'tcx QPath<'_>,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
20-
(ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => {
20+
(ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => {
2121
span_lint_and_then(
2222
cx,
2323
TRANSMUTE_PTR_TO_REF,
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
3434
("&*", "*const")
3535
};
3636

37-
let arg = if from_pty.ty == *to_ref_ty {
37+
let arg = if from_ptr_ty.ty == *to_ref_ty {
3838
arg
3939
} else {
4040
arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty)))

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,67 @@ pub(super) fn check<'tcx>(
1818
) -> bool {
1919
let mut triggered = false;
2020

21-
match (&from_ty.kind(), &to_ty.kind()) {
22-
(ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) => {
23-
if_chain! {
24-
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
25-
if let ty::Uint(ty::UintTy::U8) = slice_ty.kind();
26-
if from_mutbl == to_mutbl;
27-
then {
28-
let postfix = if *from_mutbl == Mutability::Mut {
29-
"_mut"
30-
} else {
31-
""
32-
};
21+
if let (ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) = (&from_ty.kind(), &to_ty.kind()) {
22+
if_chain! {
23+
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
24+
if let ty::Uint(ty::UintTy::U8) = slice_ty.kind();
25+
if from_mutbl == to_mutbl;
26+
then {
27+
let postfix = if *from_mutbl == Mutability::Mut {
28+
"_mut"
29+
} else {
30+
""
31+
};
3332

34-
span_lint_and_sugg(
33+
span_lint_and_sugg(
34+
cx,
35+
TRANSMUTE_BYTES_TO_STR,
36+
e.span,
37+
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
38+
"consider using",
39+
format!(
40+
"std::str::from_utf8{}({}).unwrap()",
41+
postfix,
42+
snippet(cx, args[0].span, ".."),
43+
),
44+
Applicability::Unspecified,
45+
);
46+
triggered = true;
47+
} else {
48+
if (cx.tcx.erase_regions(from_ty) != cx.tcx.erase_regions(to_ty))
49+
&& !const_context {
50+
span_lint_and_then(
3551
cx,
36-
TRANSMUTE_BYTES_TO_STR,
52+
TRANSMUTE_PTR_TO_PTR,
3753
e.span,
38-
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
39-
"consider using",
40-
format!(
41-
"std::str::from_utf8{}({}).unwrap()",
42-
postfix,
43-
snippet(cx, args[0].span, ".."),
44-
),
45-
Applicability::Unspecified,
54+
"transmute from a reference to a reference",
55+
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
56+
let ty_from_and_mut = ty::TypeAndMut {
57+
ty: ty_from,
58+
mutbl: *from_mutbl
59+
};
60+
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: *to_mutbl };
61+
let sugg_paren = arg
62+
.as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
63+
.as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
64+
let sugg = if *to_mutbl == Mutability::Mut {
65+
sugg_paren.mut_addr_deref()
66+
} else {
67+
sugg_paren.addr_deref()
68+
};
69+
diag.span_suggestion(
70+
e.span,
71+
"try",
72+
sugg.to_string(),
73+
Applicability::Unspecified,
74+
);
75+
},
4676
);
47-
triggered = true;
48-
} else {
49-
if (cx.tcx.erase_regions(from_ty) != cx.tcx.erase_regions(to_ty))
50-
&& !const_context {
51-
span_lint_and_then(
52-
cx,
53-
TRANSMUTE_PTR_TO_PTR,
54-
e.span,
55-
"transmute from a reference to a reference",
56-
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
57-
let ty_from_and_mut = ty::TypeAndMut {
58-
ty: ty_from,
59-
mutbl: *from_mutbl
60-
};
61-
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: *to_mutbl };
62-
let sugg_paren = arg
63-
.as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
64-
.as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
65-
let sugg = if *to_mutbl == Mutability::Mut {
66-
sugg_paren.mut_addr_deref()
67-
} else {
68-
sugg_paren.addr_deref()
69-
};
70-
diag.span_suggestion(
71-
e.span,
72-
"try",
73-
sugg.to_string(),
74-
Applicability::Unspecified,
75-
);
76-
},
77-
);
7877

79-
triggered = true;
80-
}
78+
triggered = true;
8179
}
8280
}
83-
},
84-
_ => {},
81+
}
8582
}
8683

8784
triggered

clippy_lints/src/use_self.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
265265
let hir = cx.tcx.hir();
266266
let id = hir.get_parent_node(hir_ty.hir_id);
267267

268-
if !hir.opt_span(id).map(in_macro).unwrap_or(false) {
268+
if !hir.opt_span(id).map_or(false, in_macro) {
269269
match hir.find(id) {
270270
Some(Node::Expr(Expr {
271271
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),

0 commit comments

Comments
 (0)