Skip to content
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

Expand, rename and improve incorrect_fn_null_checks lint #113657

Merged
merged 7 commits into from
Aug 3, 2023

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Jul 13, 2023

This PR,

  • firstly, expand the lint by now linting on references
  • secondly, it renames the lint incorrect_fn_null_checks -> useless_ptr_null_checks
  • and thirdly it improves the lint by catching ptr::from_mut, ptr::from_ref, as well as <*mut _>::cast and <*const _>::cast_mut

Fixes #113601
cc @est31

@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 13, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the expand-incorrect_fn_null_check-lint branch from 71652f6 to bbc85af Compare July 13, 2023 14:19
@est31
Copy link
Member

est31 commented Jul 13, 2023

Very good work, in the future it would be also good to lint on these:

  • Box::into_raw
  • NonNull::as_ptr
  • {str, Vec, String, slice}::as_ptr/{str, Vec, String, slice}::as_mut_ptr, although here we need to be sure they return non-null pointers for empty collections
  • {Cell,Weak,Rc,Arc}::as_ptr
  • CStr::as_ptr

etc. Maybe it might make sense to introduce a #[rustc_pointer_non_null] attribute and add it to these functions?

That's more for follow up work though.

When working on #113652 I was also briefly considering using ty_is_known_nonnull from types.rs, but that one is about types and what we really need to check for is conversions, whether it's a cast or one of the known functions. We can't just take any function on these types with a pointer return value, the function might actually do something completely different. In other words, the approach of this PR is good.

@est31
Copy link
Member

est31 commented Jul 13, 2023

That's more for follow up work though.

Especially I'd like this to get merged sooner than later as it would be nice if the lint rename wasn't exposed to stable users. Beta has just branched, so we have weeks of time, but still.

compiler/rustc_lint/src/ptr_nulls.rs Outdated Show resolved Hide resolved
@est31
Copy link
Member

est31 commented Jul 13, 2023

Approved this but won't r+ as I think it would be better for someone from the compiler team to approve it -- note that as per @scottmcm this doesn't need a lang team FCP.

@Urgau Urgau force-pushed the expand-incorrect_fn_null_check-lint branch from bbc85af to ae36b80 Compare July 13, 2023 18:48
@bors
Copy link
Contributor

bors commented Aug 1, 2023

☔ The latest upstream changes (presumably #114310) made this pull request unmergeable. Please resolve the merge conflicts.

@Urgau Urgau force-pushed the expand-incorrect_fn_null_check-lint branch from ae36b80 to de576e1 Compare August 1, 2023 18:04
compiler/rustc_lint/src/ptr_nulls.rs Show resolved Hide resolved
compiler/rustc_lint/src/ptr_nulls.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/ptr_nulls.rs Outdated Show resolved Hide resolved
pub enum PtrNullChecksDiag<'a> {
#[diag(lint_ptr_null_checks_fn_ptr)]
#[help(lint_help)]
FnPtr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the expression's span and type be added here too, for symmetry?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know, but added anyway since it doesn't make the output any worse.

@cjgillot cjgillot self-assigned this Aug 3, 2023
@Urgau Urgau force-pushed the expand-incorrect_fn_null_check-lint branch from de576e1 to ee51953 Compare August 3, 2023 08:58
@cjgillot
Copy link
Contributor

cjgillot commented Aug 3, 2023

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 3, 2023

📌 Commit ee51953 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 3, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 3, 2023
…ck-lint, r=cjgillot

Expand, rename and improve `incorrect_fn_null_checks` lint

This PR,

 - firstly, expand the lint by now linting on references
 - secondly, it renames the lint `incorrect_fn_null_checks` -> `useless_ptr_null_checks`
 - and thirdly it improves the lint by catching `ptr::from_mut`, `ptr::from_ref`, as well as `<*mut _>::cast` and `<*const _>::cast_mut`

Fixes rust-lang#113601
cc `@est31`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 3, 2023
…ck-lint, r=cjgillot

Expand, rename and improve `incorrect_fn_null_checks` lint

This PR,

 - firstly, expand the lint by now linting on references
 - secondly, it renames the lint `incorrect_fn_null_checks` -> `useless_ptr_null_checks`
 - and thirdly it improves the lint by catching `ptr::from_mut`, `ptr::from_ref`, as well as `<*mut _>::cast` and `<*const _>::cast_mut`

Fixes rust-lang#113601
cc ``@est31``
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 3, 2023
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#113657 (Expand, rename and improve `incorrect_fn_null_checks` lint)
 - rust-lang#114237 (parser: more friendly hints for handling `async move` in the 2015 edition)
 - rust-lang#114300 (Suggests turbofish in patterns)
 - rust-lang#114372 (const validation: point at where we found a pointer but expected an integer)
 - rust-lang#114395 ([rustc_span][perf] Hoist lookup sorted by words out of the loop.)
 - rust-lang#114403 (fix the span in the suggestion of remove question mark)
 - rust-lang#114408 (Temporary remove myself from review rotation)
 - rust-lang#114415 (Skip checking of `rustc_codegen_gcc` with vendoring enabled)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7518ae5 into rust-lang:master Aug 3, 2023
@rustbot rustbot added this to the 1.73.0 milestone Aug 3, 2023
@Urgau Urgau deleted the expand-incorrect_fn_null_check-lint branch August 4, 2023 07:09
est31 added a commit to est31/rust that referenced this pull request Aug 5, 2023
It was added by rust-lang#113657 for its purposes.
Now it is not used any more, remove it,
as we use the attr now.
flip1995 pushed a commit to flip1995/rust that referenced this pull request Aug 11, 2023
…ck-lint, r=cjgillot

Expand, rename and improve `incorrect_fn_null_checks` lint

This PR,

 - firstly, expand the lint by now linting on references
 - secondly, it renames the lint `incorrect_fn_null_checks` -> `useless_ptr_null_checks`
 - and thirdly it improves the lint by catching `ptr::from_mut`, `ptr::from_ref`, as well as `<*mut _>::cast` and `<*const _>::cast_mut`

Fixes rust-lang#113601
cc ```@est31```
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 16, 2023
…, r=jackh726

Make useless_ptr_null_checks smarter about some std functions

This teaches the `useless_ptr_null_checks` lint that some std functions can't ever return null pointers, because they need to point to valid data, get references as input, etc.

This is achieved by introducing an `#[rustc_never_returns_null_ptr]` attribute and adding it to these std functions (gated behind bootstrap `cfg_attr`).

Later on, the attribute could maybe be used to tell LLVM that the returned pointer is never null. I don't expect much impact of that though, as the functions are pretty shallow and usually the input data is already never null.

Follow-up of PR rust-lang#113657

Fixes rust-lang#114442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend incorrect_fn_null_checks lint to useless_ptr_null_checks
7 participants