forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#106266 - matthiaskrgr:rollup-cxrdbzy, r=matth…
…iaskrgr Rollup of 9 pull requests Successful merges: - rust-lang#104531 (Provide a better error and a suggestion for `Fn` traits with lifetime params) - rust-lang#105899 (`./x doc library --open` opens `std`) - rust-lang#106190 (Account for multiple multiline spans with empty padding) - rust-lang#106202 (Trim more paths in obligation types) - rust-lang#106234 (rustdoc: simplify settings, help, and copy button CSS by not reusing) - rust-lang#106236 (docs/test: add docs and a UI test for `E0514` and `E0519`) - rust-lang#106259 (Update Clippy) - rust-lang#106260 (Fix index out of bounds issues in rustdoc) - rust-lang#106263 (Formatter should not try to format non-Rust files) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
- Loading branch information
Showing
99 changed files
with
2,558 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::{is_integer_literal, is_path_diagnostic_item}; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for comparing a function pointer to null. | ||
/// | ||
/// ### Why is this bad? | ||
/// Function pointers are assumed to not be null. | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// let fn_ptr: fn() = /* somehow obtained nullable function pointer */ | ||
/// | ||
/// if (fn_ptr as *const ()).is_null() { ... } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// let fn_ptr: Option<fn()> = /* somehow obtained nullable function pointer */ | ||
/// | ||
/// if fn_ptr.is_none() { ... } | ||
/// ``` | ||
#[clippy::version = "1.67.0"] | ||
pub FN_NULL_CHECK, | ||
correctness, | ||
"`fn()` type assumed to be nullable" | ||
} | ||
declare_lint_pass!(FnNullCheck => [FN_NULL_CHECK]); | ||
|
||
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
span_lint_and_help( | ||
cx, | ||
FN_NULL_CHECK, | ||
expr.span, | ||
"function pointer assumed to be nullable, even though it isn't", | ||
None, | ||
"try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value", | ||
); | ||
} | ||
|
||
fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
if let ExprKind::Cast(cast_expr, cast_ty) = expr.kind | ||
&& let TyKind::Ptr(_) = cast_ty.kind | ||
{ | ||
cx.typeck_results().expr_ty_adjusted(cast_expr).is_fn() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for FnNullCheck { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
match expr.kind { | ||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>).is_null() | ||
ExprKind::MethodCall(method_name, receiver, _, _) | ||
if method_name.ident.as_str() == "is_null" && is_fn_ptr_cast(cx, receiver) => | ||
{ | ||
lint_expr(cx, expr); | ||
}, | ||
|
||
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => { | ||
let to_check: &Expr<'_>; | ||
if is_fn_ptr_cast(cx, left) { | ||
to_check = right; | ||
} else if is_fn_ptr_cast(cx, right) { | ||
to_check = left; | ||
} else { | ||
return; | ||
} | ||
|
||
match to_check.kind { | ||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>) | ||
ExprKind::Cast(cast_expr, _) if is_integer_literal(cast_expr, 0) => { | ||
lint_expr(cx, expr); | ||
}, | ||
|
||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null() | ||
ExprKind::Call(func, []) if is_path_diagnostic_item(cx, func, sym::ptr_null) => { | ||
lint_expr(cx, expr); | ||
}, | ||
|
||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr> | ||
_ if matches!( | ||
constant(cx, cx.typeck_results(), to_check), | ||
Some((Constant::RawPtr(0), _)) | ||
) => | ||
{ | ||
lint_expr(cx, expr); | ||
}, | ||
|
||
_ => {}, | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.