Conversation
|
Some changes occurred in integer formatting cc @tgross35
cc @Amanieu, @folkertdev, @sayantn |
|
r? @adwinwhite rustbot has assigned @adwinwhite. Use Why was this reviewer chosen?The reviewer was selected based on:
|
fd65c7b to
2eebb0a
Compare
There was a problem hiding this comment.
So this is just checking if any argument is passed to any unsafe function? I think that's going to be far too noisy and a better fit for Clippy, it looks like a lot of false positives.
Maybe there's more of a case for uplifting clippy::not_unsafe_ptr_arg_deref? I think that's more along the lines of what Josh was suggesting at #162090 (comment).
Also there are no tests or examples, this is hard to evaluate.
| declare_lint! { | ||
| /// The `safe_fn_direct_use_of_unsafe_op_on_args` lint detects when unsafe operations | ||
| /// are trivially used on a function's arguments, and this function is safe and pub. | ||
| /// which might cause [undefined behavior]. | ||
| pub SAFE_FN_DIRECT_USE_OF_UNSAFE_OP_ON_ARGS, | ||
| Warn, | ||
| "detects when a trivial unsafe operation is used on arguments, from a safe fn" | ||
| } |
There was a problem hiding this comment.
Please always include examples and a more thorough explanation with declare_lint. In particular, what makes something "trivial" could be clarified.
| #[allow(safe_fn_direct_use_of_unsafe_op_on_args)] | ||
| pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T { |
There was a problem hiding this comment.
Always use expect rather than allow unless it's unavoidable, so we don't keep around allows that we no longer need. The reason would be nice too
| pub fn for_each_expr<'tcx>( | ||
| tcx: TyCtxt<'tcx>, | ||
| node: &'tcx hir::Expr<'tcx>, | ||
| f: impl FnMut(&'tcx hir::Expr<'tcx>), | ||
| ) { |
There was a problem hiding this comment.
Docs please. Could this be pub(crate) or not pub at all?
| #[derive(Diagnostic)] | ||
| #[diag("call to an unsafe function on an argument of a safe function")] | ||
| pub(crate) struct BuiltinSafeFnDirectUseOfUnsafeOpOnArgs { | ||
| #[label("this code might cause undefined behavior when executed")] | ||
| pub label: Span, | ||
| } |
There was a problem hiding this comment.
This sort of thing could use a help
| /// ``` | ||
| #[stable(feature = "push_mut", since = "1.95.0")] | ||
| #[must_use = "if you don't need a reference to the value, use `VecDeque::insert` instead"] | ||
| #[allow(safe_fn_direct_use_of_unsafe_op_on_args)] |
There was a problem hiding this comment.
Why does this lint trigger here? This function an assertion and early exit to catch the unsafe cases, ensuring that the function is actually safe. I think the lint should only trigger when the safety obligations of the unsafe operation are obviously not discharged by an assertion or early-out. If the standard library already has this many allows, I can't imagine how many false-positives it has for user code.
This comment has been minimized.
This comment has been minimized.
Yep, it is.
Yeah, this is also what I wanted to discuss in this PR. Initially, as the issue was still labeled What do you want to do for this lint ? If all of you agree to move this to clippy instead, I can propose another PR for improving the existing lint in clippy, then.
I totally agree, depending on what we decide here, I am going to fix it. |
…ument This adds a new lint for emitting a warning when unsafe ops are directly used on a public safe function's arguments, which might cause undefined behaviors.
2eebb0a to
d9299af
Compare
We could also reduce the scope of this lint to warn only about direct use of
I have just added a test for this, which shows most of the supported cases. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
The way to get a concrete answer is to nominate for lang, but it feels a bit early for that. Perhaps start with a discussion at #t-lang to figure out an exact proposal? |
|
☔ The latest upstream changes (presumably #163227) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
fixes #162090
This is a first proposal.
I am opened to suggestions.