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

Refactor: arrange transmute lints #6716

Merged
merged 17 commits into from
Mar 2, 2021

Conversation

magurotuna
Copy link
Contributor

This PR arranges transmute lints so that they can be accessed more easily.
Basically, I followed the instruction described in #6680 as to how to do the refactoring.

  • declare_clippy_lint! and impl LintPass is placed in transmute/mod.rs
  • Uitlity functions is placed in transmute/utils.rs
  • Each lint function about transmute is moved into its own module, like transmute/useless_transmute.rs

For ease of review, I refactored step by step, keeping each commit small. For instance, all I did in
2451781 was to move useless_transmute into its own module.


changelog: Refactor transmute.rs file into transmute module.

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 11, 2021
Comment on lines 342 to 355
if useless_transmute::check(cx, e, from_ty, to_ty, args) {
return;
}

let triggered = wrong_transmute::check(cx, e, from_ty, to_ty) ||
crosspointer_transmute::check(cx, e, from_ty, to_ty) ||
transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, args, qpath) ||
transmute_int_to_char::check(cx, e, from_ty, to_ty, args) ||
transmute_ref_to_ref::check(cx, e, from_ty, to_ty, args, const_context) ||
transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args) ||
transmute_int_to_bool::check(cx, e, from_ty, to_ty, args) ||
transmute_int_to_float::check(cx, e, from_ty, to_ty, args, const_context) ||
transmute_float_to_int::check(cx, e, from_ty, to_ty, args, const_context) ||
unsound_collection_transmute::check(cx, e, from_ty, to_ty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a question.
These lines seem to implicitly define the priority order of the lints.
For example, useless_transmute > others and wrong_transmute > crosspointer_transmute and so on.

Is it ok? I think this may become a problem if a new lint has a mutual condition that triggers both the lint and another lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point.
Generally, all the lints here are exclusive; if a code violates transmute_int_to_char, then this code does never trigger transmute_ptr_to_ptr, transmute_int_to_bool, and so on.
But there are a few exceptions. For example, useless_transmute and transmute_ref_to_ref can potentially be triggered at the same time - this is NOT exclusive. In such a case, I think we'd prefer one diagnostic over multiple ones to avoid getting the programmer overwhelmed. This is why the lint priority is necessary. (Also... the test cases would fail if we removed the priority :-|)

If a new lint related to transmute is added in the future, we will have to consider the priority of it to put it in the appropriate position. If anyone comes up with a better way, that would be really welcome.

Copy link
Contributor

@Y-Nak Y-Nak Feb 11, 2021

Choose a reason for hiding this comment

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

In such a case, I think we'd prefer one diagnostic over multiple ones to avoid getting the programmer overwhelmed.

I agree with you in almost all cases. My concern here is that if #[allow(clippy::useless_transmute)] is specified by the user, then transmute_ref_to_ref may also be suppressed unexpectedly.

I feel like this is a problem that should be generalized (may be as a future task).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My concern here is that if #[allow(clippy::useless_transmute)] is specified by the user, then transmute_ref_to_ref may also be suppressed unexpectedly.

Ah that makes much sense. I guess the current master branch also has that problem; once the lint checking flow goes into useless_transmute, the other lints won't never be checked even if #[allow(clippy::useless_transmute)] is specified.
With this fact in mind, I feel like prioritizing the lints might be bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd say it would be better to keep the current behavior in this PR by confirming the existing test cases are passing, because the main focus of this PR is refactoring. We can consider further how to address the priority issue in another issue or PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

the main focus of this PR is refactoring.

I agree with you! I created the topic to discuss this issue on zulip

Copy link
Member

Choose a reason for hiding this comment

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

If two lints cannot conflict, I would avoid short circuiting them like this. Only if one lint triggering should depend on another lint this should be done. So I would write this as

let mut linted = check_lint_A();
linted |= check_lint_B(); // no dependency
linted |= check_lint_C() || check_lint_D(); // D depends on C
...

if !linted {
    check_lint_XYZ(); // XYZ depends on ALL lints
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@flip1995 Looks cool. I'll rewrite the code!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@flip1995 Rewriting and rebasing with master are done. PTAL :)

@bors
Copy link
Contributor

bors commented Feb 11, 2021

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

@magurotuna magurotuna force-pushed the refactor-transmute-mod branch from 81ce279 to 9d937c9 Compare February 12, 2021 12:44
@flip1995
Copy link
Member

Since the other 2 PRs are assigned to me, I might as well also review this.

r? @flip1995

@rust-highfive rust-highfive assigned flip1995 and unassigned llogiq Feb 12, 2021
Copy link
Contributor

@Y-Nak Y-Nak left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

LGTM. This reminded me of my laziness declaring all transmute lints as Applicability::Unspecified. Now that it's easier to see what's going on, we should revisit this.

clippy_lints/src/transmute/transmute_ptr_to_ref.rs Outdated Show resolved Hide resolved
@flip1995 flip1995 force-pushed the refactor-transmute-mod branch from 9d937c9 to bf00098 Compare March 2, 2021 09:42
@flip1995
Copy link
Member

flip1995 commented Mar 2, 2021

@bors r+ p=1

Thanks! I will go through the refactor PRs this week one by one, so that you don't have to keep rebasing every other day

@bors
Copy link
Contributor

bors commented Mar 2, 2021

📌 Commit bf00098 has been approved by flip1995

@bors
Copy link
Contributor

bors commented Mar 2, 2021

⌛ Testing commit bf00098 with merge 3cd6ca0...

@bors
Copy link
Contributor

bors commented Mar 2, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 3cd6ca0 to master...

@bors bors merged commit 3cd6ca0 into rust-lang:master Mar 2, 2021
@magurotuna magurotuna deleted the refactor-transmute-mod branch March 2, 2021 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants