Skip to content

Commit 0853540

Browse files
committed
Merge remote-tracking branch 'upstream/master' into rustup
2 parents 2444494 + 22af8fe commit 0853540

File tree

107 files changed

+2596
-399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+2596
-399
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,6 +4203,7 @@ Released 2018-09-13
42034203
[`float_cmp_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp_const
42044204
[`float_equality_without_abs`]: https://rust-lang.github.io/rust-clippy/master/index.html#float_equality_without_abs
42054205
[`fn_address_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_address_comparisons
4206+
[`fn_null_check`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_null_check
42064207
[`fn_params_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_params_excessive_bools
42074208
[`fn_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast
42084209
[`fn_to_numeric_cast_any`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_any
@@ -4460,6 +4461,7 @@ Released 2018-09-13
44604461
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
44614462
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
44624463
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
4464+
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
44634465
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
44644466
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
44654467
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
@@ -4545,6 +4547,7 @@ Released 2018-09-13
45454547
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match
45464548
[`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else
45474549
[`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count
4550+
[`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref
45484551
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
45494552
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
45504553
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
@@ -4590,6 +4593,7 @@ Released 2018-09-13
45904593
[`transmute_int_to_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_bool
45914594
[`transmute_int_to_char`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_char
45924595
[`transmute_int_to_float`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_float
4596+
[`transmute_null_to_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_null_to_fn
45934597
[`transmute_num_to_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_num_to_bytes
45944598
[`transmute_ptr_to_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
45954599
[`transmute_ptr_to_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ref

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ filetime = "0.2"
4242
rustc-workspace-hack = "1.0"
4343

4444
# UI test dependencies
45+
clap = { version = "3.1", features = ["derive"] }
4546
clippy_utils = { path = "clippy_utils" }
4647
derive-new = "0.5"
4748
if_chain = "1.0"

clippy_lints/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ quine-mc_cluskey = "0.2"
1919
regex-syntax = "0.6"
2020
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = { version = "1.0", optional = true }
22-
tempfile = { version = "3.2", optional = true }
22+
tempfile = { version = "3.3.0", optional = true }
2323
toml = "0.5"
2424
unicode-normalization = "0.1"
2525
unicode-script = { version = "0.5", default-features = false }

clippy_lints/src/blocks_in_if_conditions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
8585
);
8686
}
8787
} else {
88-
let span =
89-
block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
88+
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
9089
if span.from_expansion() || expr.span.from_expansion() {
9190
return;
9291
}

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv
5454

5555
diag.span_suggestion(
5656
expr.span,
57-
&format!("replace with `ptr::slice_from_raw_parts{mutbl_fn_str}`"),
57+
format!("replace with `ptr::slice_from_raw_parts{mutbl_fn_str}`"),
5858
sugg,
5959
rustc_errors::Applicability::HasPlaceholders,
6060
);

clippy_lints/src/declared_lints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
161161
crate::float_literal::LOSSY_FLOAT_LITERAL_INFO,
162162
crate::floating_point_arithmetic::IMPRECISE_FLOPS_INFO,
163163
crate::floating_point_arithmetic::SUBOPTIMAL_FLOPS_INFO,
164+
crate::fn_null_check::FN_NULL_CHECK_INFO,
164165
crate::format::USELESS_FORMAT_INFO,
165166
crate::format_args::FORMAT_IN_FORMAT_ARGS_INFO,
166167
crate::format_args::TO_STRING_IN_FORMAT_ARGS_INFO,
@@ -494,6 +495,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
494495
crate::pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE_INFO,
495496
crate::pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF_INFO,
496497
crate::pattern_type_mismatch::PATTERN_TYPE_MISMATCH_INFO,
498+
crate::permissions_set_readonly_false::PERMISSIONS_SET_READONLY_FALSE_INFO,
497499
crate::precedence::PRECEDENCE_INFO,
498500
crate::ptr::CMP_NULL_INFO,
499501
crate::ptr::INVALID_NULL_PTR_USAGE_INFO,
@@ -535,6 +537,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
535537
crate::single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES_INFO,
536538
crate::single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS_INFO,
537539
crate::size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT_INFO,
540+
crate::size_of_ref::SIZE_OF_REF_INFO,
538541
crate::slow_vector_initialization::SLOW_VECTOR_INITIALIZATION_INFO,
539542
crate::std_instead_of_core::ALLOC_INSTEAD_OF_CORE_INFO,
540543
crate::std_instead_of_core::STD_INSTEAD_OF_ALLOC_INFO,
@@ -568,6 +571,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
568571
crate::transmute::TRANSMUTE_INT_TO_BOOL_INFO,
569572
crate::transmute::TRANSMUTE_INT_TO_CHAR_INFO,
570573
crate::transmute::TRANSMUTE_INT_TO_FLOAT_INFO,
574+
crate::transmute::TRANSMUTE_NULL_TO_FN_INFO,
571575
crate::transmute::TRANSMUTE_NUM_TO_BYTES_INFO,
572576
crate::transmute::TRANSMUTE_PTR_TO_PTR_INFO,
573577
crate::transmute::TRANSMUTE_PTR_TO_REF_INFO,

clippy_lints/src/dereference.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,10 +1282,10 @@ fn referent_used_exactly_once<'tcx>(
12821282
possible_borrowers.push((body_owner_local_def_id, PossibleBorrowerMap::new(cx, mir)));
12831283
}
12841284
let possible_borrower = &mut possible_borrowers.last_mut().unwrap().1;
1285-
// If `only_borrowers` were used here, the `copyable_iterator::warn` test would fail. The reason is
1286-
// that `PossibleBorrowerVisitor::visit_terminator` considers `place.local` a possible borrower of
1287-
// itself. See the comment in that method for an explanation as to why.
1288-
possible_borrower.bounded_borrowers(&[local], &[local, place.local], place.local, location)
1285+
// If `place.local` were not included here, the `copyable_iterator::warn` test would fail. The
1286+
// reason is that `PossibleBorrowerVisitor::visit_terminator` considers `place.local` a possible
1287+
// borrower of itself. See the comment in that method for an explanation as to why.
1288+
possible_borrower.at_most_borrowers(cx, &[local, place.local], place.local, location)
12891289
&& used_exactly_once(mir, place.local).unwrap_or(false)
12901290
} else {
12911291
false

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
324324
let maybe_neg_sugg = |expr, hir_id| {
325325
let sugg = Sugg::hir(cx, expr, "..");
326326
if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
327-
format!("-{sugg}")
327+
format!("-{}", sugg.maybe_par())
328328
} else {
329329
sugg.to_string()
330330
}

clippy_lints/src/fn_null_check.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use clippy_utils::consts::{constant, Constant};
2+
use clippy_utils::diagnostics::span_lint_and_help;
3+
use clippy_utils::{is_integer_literal, is_path_diagnostic_item};
4+
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::sym;
8+
9+
declare_clippy_lint! {
10+
/// ### What it does
11+
/// Checks for comparing a function pointer to null.
12+
///
13+
/// ### Why is this bad?
14+
/// Function pointers are assumed to not be null.
15+
///
16+
/// ### Example
17+
/// ```rust,ignore
18+
/// let fn_ptr: fn() = /* somehow obtained nullable function pointer */
19+
///
20+
/// if (fn_ptr as *const ()).is_null() { ... }
21+
/// ```
22+
/// Use instead:
23+
/// ```rust,ignore
24+
/// let fn_ptr: Option<fn()> = /* somehow obtained nullable function pointer */
25+
///
26+
/// if fn_ptr.is_none() { ... }
27+
/// ```
28+
#[clippy::version = "1.67.0"]
29+
pub FN_NULL_CHECK,
30+
correctness,
31+
"`fn()` type assumed to be nullable"
32+
}
33+
declare_lint_pass!(FnNullCheck => [FN_NULL_CHECK]);
34+
35+
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
36+
span_lint_and_help(
37+
cx,
38+
FN_NULL_CHECK,
39+
expr.span,
40+
"function pointer assumed to be nullable, even though it isn't",
41+
None,
42+
"try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value",
43+
);
44+
}
45+
46+
fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
47+
if let ExprKind::Cast(cast_expr, cast_ty) = expr.kind
48+
&& let TyKind::Ptr(_) = cast_ty.kind
49+
{
50+
cx.typeck_results().expr_ty_adjusted(cast_expr).is_fn()
51+
} else {
52+
false
53+
}
54+
}
55+
56+
impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
57+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
58+
match expr.kind {
59+
// Catching:
60+
// (fn_ptr as *<const/mut> <ty>).is_null()
61+
ExprKind::MethodCall(method_name, receiver, _, _)
62+
if method_name.ident.as_str() == "is_null" && is_fn_ptr_cast(cx, receiver) =>
63+
{
64+
lint_expr(cx, expr);
65+
},
66+
67+
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => {
68+
let to_check: &Expr<'_>;
69+
if is_fn_ptr_cast(cx, left) {
70+
to_check = right;
71+
} else if is_fn_ptr_cast(cx, right) {
72+
to_check = left;
73+
} else {
74+
return;
75+
}
76+
77+
match to_check.kind {
78+
// Catching:
79+
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>)
80+
ExprKind::Cast(cast_expr, _) if is_integer_literal(cast_expr, 0) => {
81+
lint_expr(cx, expr);
82+
},
83+
84+
// Catching:
85+
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null()
86+
ExprKind::Call(func, []) if is_path_diagnostic_item(cx, func, sym::ptr_null) => {
87+
lint_expr(cx, expr);
88+
},
89+
90+
// Catching:
91+
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
92+
_ if matches!(
93+
constant(cx, cx.typeck_results(), to_check),
94+
Some((Constant::RawPtr(0), _))
95+
) =>
96+
{
97+
lint_expr(cx, expr);
98+
},
99+
100+
_ => {},
101+
}
102+
},
103+
_ => {},
104+
}
105+
}
106+
}

clippy_lints/src/format_args.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,7 @@ fn outermost_expn_data(expn_data: ExpnData) -> ExpnData {
362362
}
363363
}
364364

365-
fn check_format_in_format_args(
366-
cx: &LateContext<'_>,
367-
call_site: Span,
368-
name: Symbol,
369-
arg: &Expr<'_>,
370-
) {
365+
fn check_format_in_format_args(cx: &LateContext<'_>, call_site: Span, name: Symbol, arg: &Expr<'_>) {
371366
let expn_data = arg.span.ctxt().outer_expn_data();
372367
if expn_data.call_site.from_expansion() {
373368
return;
@@ -382,7 +377,7 @@ fn check_format_in_format_args(
382377
call_site,
383378
&format!("`format!` in `{name}!` args"),
384379
|diag| {
385-
diag.help(&format!(
380+
diag.help(format!(
386381
"combine the `format!(..)` arguments with the outer `{name}!(..)` call"
387382
));
388383
diag.help("or consider changing `format!` to `format_args!`");
@@ -440,7 +435,10 @@ fn check_to_string_in_format_args(cx: &LateContext<'_>, name: Symbol, value: &Ex
440435

441436
/// Returns true if `hir_id` is referred to by multiple format params
442437
fn is_aliased(args: &FormatArgsExpn<'_>, hir_id: HirId) -> bool {
443-
args.params().filter(|param| param.value.hir_id == hir_id).at_most_one().is_err()
438+
args.params()
439+
.filter(|param| param.value.hir_id == hir_id)
440+
.at_most_one()
441+
.is_err()
444442
}
445443

446444
fn count_needed_derefs<'tcx, I>(mut ty: Ty<'tcx>, mut iter: I) -> (usize, Ty<'tcx>)
@@ -450,7 +448,11 @@ where
450448
let mut n_total = 0;
451449
let mut n_needed = 0;
452450
loop {
453-
if let Some(Adjustment { kind: Adjust::Deref(overloaded_deref), target }) = iter.next() {
451+
if let Some(Adjustment {
452+
kind: Adjust::Deref(overloaded_deref),
453+
target,
454+
}) = iter.next()
455+
{
454456
n_total += 1;
455457
if overloaded_deref.is_some() {
456458
n_needed = n_total;

0 commit comments

Comments
 (0)