-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand, rename and improve incorrect_fn_null_checks
lint
#113657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 7 commits into
rust-lang:master
from
Urgau:expand-incorrect_fn_null_check-lint
Aug 3, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
743ae5a
Expand incorrect_fn_null_check lint with reference null checking
Urgau d2b7c80
Rename incorrect_fn_null_checks to useless_ptr_null_checks
Urgau 84c5372
Rename incorrect_fn_null_checks to useless_ptr_null_checks (clippy side)
Urgau ef3413d
Add more tests for useless_ptr_null_checks lint
Urgau 0b9529c
Add diagnostic items for `<*const _>::cast` and `ptr::from_mut`
Urgau 4b3dadb
Also lint on cast/cast_mut and ptr::from_mut/ptr::from_ref
Urgau ee51953
Also add label with original type for function pointers
Urgau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,146 @@ | ||
use crate::{lints::PtrNullChecksDiag, LateContext, LateLintPass, LintContext}; | ||
use rustc_ast::LitKind; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind}; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
use rustc_span::sym; | ||
|
||
declare_lint! { | ||
/// The `useless_ptr_null_checks` lint checks for useless null checks against pointers | ||
/// obtained from non-null types. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # fn test() {} | ||
/// let fn_ptr: fn() = /* somehow obtained nullable function pointer */ | ||
/// # test; | ||
/// | ||
/// if (fn_ptr as *const ()).is_null() { /* ... */ } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Function pointers and references are assumed to be non-null, checking them for null | ||
/// will always return false. | ||
USELESS_PTR_NULL_CHECKS, | ||
Warn, | ||
"useless checking of non-null-typed pointer" | ||
} | ||
|
||
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]); | ||
|
||
/// This function detects and returns the original expression from a series of consecutive casts, | ||
/// ie. `(my_fn as *const _ as *mut _).cast_mut()` would return the expression for `my_fn`. | ||
fn ptr_cast_chain<'a>(cx: &'a LateContext<'_>, mut e: &'a Expr<'a>) -> Option<&'a Expr<'a>> { | ||
let mut had_at_least_one_cast = false; | ||
loop { | ||
e = e.peel_blocks(); | ||
e = if let ExprKind::Cast(expr, t) = e.kind | ||
&& let TyKind::Ptr(_) = t.kind { | ||
had_at_least_one_cast = true; | ||
expr | ||
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind | ||
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) | ||
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_cast | sym::ptr_cast_mut)) { | ||
had_at_least_one_cast = true; | ||
expr | ||
} else if let ExprKind::Call(path, [arg]) = e.kind | ||
&& let ExprKind::Path(ref qpath) = path.kind | ||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | ||
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_from_ref | sym::ptr_from_mut)) { | ||
had_at_least_one_cast = true; | ||
arg | ||
} else if had_at_least_one_cast { | ||
return Some(e); | ||
} else { | ||
return None; | ||
}; | ||
} | ||
} | ||
|
||
fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullChecksDiag<'a>> { | ||
let expr = ptr_cast_chain(cx, expr)?; | ||
|
||
let orig_ty = cx.typeck_results().expr_ty(expr); | ||
if orig_ty.is_fn() { | ||
Some(PtrNullChecksDiag::FnPtr { orig_ty, label: expr.span }) | ||
} else if orig_ty.is_ref() { | ||
Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span }) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for PtrNullChecks { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
match expr.kind { | ||
// Catching: | ||
// <*<const/mut> <ty>>::is_null(fn_ptr as *<const/mut> <ty>) | ||
ExprKind::Call(path, [arg]) | ||
if let ExprKind::Path(ref qpath) = path.kind | ||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | ||
&& matches!( | ||
cx.tcx.get_diagnostic_name(def_id), | ||
Some(sym::ptr_const_is_null | sym::ptr_is_null) | ||
) | ||
&& let Some(diag) = incorrect_check(cx, arg) => | ||
{ | ||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag) | ||
} | ||
|
||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>).is_null() | ||
ExprKind::MethodCall(_, receiver, _, _) | ||
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) | ||
&& matches!( | ||
cx.tcx.get_diagnostic_name(def_id), | ||
Some(sym::ptr_const_is_null | sym::ptr_is_null) | ||
) | ||
&& let Some(diag) = incorrect_check(cx, receiver) => | ||
{ | ||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag) | ||
} | ||
|
||
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => { | ||
let to_check: &Expr<'_>; | ||
let diag: PtrNullChecksDiag<'_>; | ||
if let Some(ddiag) = incorrect_check(cx, left) { | ||
to_check = right; | ||
diag = ddiag; | ||
} else if let Some(ddiag) = incorrect_check(cx, right) { | ||
to_check = left; | ||
diag = ddiag; | ||
} else { | ||
return; | ||
} | ||
|
||
match to_check.kind { | ||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>) | ||
ExprKind::Cast(cast_expr, _) | ||
if let ExprKind::Lit(spanned) = cast_expr.kind | ||
&& let LitKind::Int(v, _) = spanned.node && v == 0 => | ||
{ | ||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag) | ||
}, | ||
|
||
// Catching: | ||
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null() | ||
ExprKind::Call(path, []) | ||
if let ExprKind::Path(ref qpath) = path.kind | ||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | ||
&& let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id) | ||
&& (diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut) => | ||
{ | ||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag) | ||
}, | ||
|
||
_ => {}, | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.