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

Add assert message to suggestion in lint assertions_on_constants #4635

Merged
merged 4 commits into from
Oct 9, 2019

Conversation

Lythenas
Copy link
Contributor

@Lythenas Lythenas commented Oct 5, 2019

  • suggest replacing assert!(false, "msg") with panic!("msg")
  • extend to allow variables any expression for "msg"
  • suggest replacing assert!(false, "msg {}", "arg") with panic!("msg {}", "arg")

changelog: add assert message to suggestion in lint assertions_on_constants

Work towards fixing: #3575

@Lythenas
Copy link
Contributor Author

Lythenas commented Oct 5, 2019

Currently I just reused the help message. I'm not 100% sure if this should be a suggestion.

@Lythenas Lythenas changed the title Add assert message to suggestion in lint assertions_on_constants WIP: Add assert message to suggestion in lint assertions_on_constants Oct 5, 2019
@Lythenas Lythenas marked this pull request as ready for review October 5, 2019 15:04
@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Oct 5, 2019
@flip1995
Copy link
Member

flip1995 commented Oct 5, 2019

You created the if_chain! with the author lint, right? These if_chains normally can e simplified. If that's not possible, it would be great if you could use more telling names, than block2, lit3, ...

@flip1995
Copy link
Member

flip1995 commented Oct 5, 2019

Also you should be able to reuse this code from here to the end of the file:

fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arms: &'a [Arm]) -> Option<String> {

@Lythenas
Copy link
Contributor Author

Lythenas commented Oct 6, 2019

You created the if_chain! with the author lint, right? These if_chains normally can e simplified. If that's not possible, it would be great if you could use more telling names, than block2, lit3, ...

Yes I created it originally from the code expanded by -Zunpretty=hir. But I had to change a few thing to get it to work.

I will change the names and try to simplify a bit. I'm not 100% sure what can be simplified and what's important but I guess the risk of someone accidentally writing the same code the macro generates are very slim (or maybe impossible because of the DropTemps and $crate in the path to begin_panic).

Should I for example keep things like ExprKind::Match(..., MatchSource::IfDesugar { contains_else_clause: false }) or simplify them to just ExprKind::Match(..., _)?

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.

I've gone over the if_chain and left you some comments on things, that I think can be removed or should be renamed.

Since the match is pretty specific for this expansion if you only match on the "happy path", that leads to the begin_panic call, I don't think that you also have to match the unrelated expanded parts. Also DropTemps is only possible in expanded code and not in hand written code (IIUC), so we should be good here.

clippy_lints/src/assertions_on_constants.rs Outdated Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Outdated Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Outdated Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Outdated Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Outdated Show resolved Hide resolved
@flip1995
Copy link
Member

flip1995 commented Oct 7, 2019

Maybe this would be worth adding to the utils.

Yes, that would be great! While your at it, can you also remove this function:

pub fn resolve_node(cx: &LateContext<'_, '_>, qpath: &QPath, id: HirId) -> Res {
cx.tables.qpath_res(qpath, id)
}

and use cx.tables.qpath_res(..) directly?

On the other hand, I think this refactor should be done in another PR. So

  1. Remove resolve_node(..)
  2. Use cx.tables.qpath_res(..) directly
  3. Put match_function_call in utils
  4. grep for match_def_path and replace it with match_function_call where applicable.

I think this is too big of a change for this unrelated PR.

@flip1995
Copy link
Member

flip1995 commented Oct 7, 2019

I'd suggest to do the refactor from my comment above and

suggest replacing assert!(false, "msg {}", "arg") with panic!("msg {}", "arg")

in another PR.

If you want to, you can do

extend to allow variables for "msg"

in this PR or also in another PR, as off your choosing.

@Lythenas Lythenas force-pushed the suggestions-for-assert-false branch from dba6723 to d66acc2 Compare October 7, 2019 17:13
@Lythenas
Copy link
Contributor Author

Lythenas commented Oct 7, 2019

I think I will do the following in this PR:

  • Keep the match_function_call in assertions_on_constants.rs
  • replace resolve_node in assertions_on_constants.rs
  • and finish up suggest replacing assert!(false, "msg") with panic!("msg")
  • extend to allow variables for "msg"

And then split the rest into other PRs:

    • Move match_function_call to utils
    • Use match_function_call in other lints where possible
    • remove resolve_node (maybe also in a separate PR)
    • suggest replacing assert!(false, "msg {}", "arg") with panic!("msg {}", "arg")

@flip1995
Copy link
Member

flip1995 commented Oct 8, 2019

Thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 8, 2019

📋 Looks like this PR is still in progress, ignoring approval.

Hint: Remove WIP from this PR's title when it is ready for review.

@flip1995
Copy link
Member

flip1995 commented Oct 8, 2019

ping @Lythenas IIUC this PR is finished. Since this is still marked as WIP, are there still open points you want to address?

@Lythenas Lythenas changed the title WIP: Add assert message to suggestion in lint assertions_on_constants Add assert message to suggestion in lint assertions_on_constants Oct 8, 2019
@Lythenas
Copy link
Contributor Author

Lythenas commented Oct 8, 2019

Sorry forgot to remove the WIP. I'm all done here.

@flip1995
Copy link
Member

flip1995 commented Oct 9, 2019

Thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 9, 2019

📌 Commit 6ee8d75 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Oct 9, 2019

⌛ Testing commit 6ee8d75 with merge d97fbdb...

bors added a commit that referenced this pull request Oct 9, 2019
Add assert message to suggestion in lint assertions_on_constants

<!--
Thank you for making Clippy better!

We're collecting our changelog from pull request descriptions.
If your PR only updates to the latest nightly, you can leave the
`changelog` entry as `none`. Otherwise, please write a short comment
explaining your change.

If your PR fixes an issue, you can add "fixes #issue_number" into this
PR description. This way the issue will be automatically closed when
your PR is merged.

If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.

- [x] Followed [lint naming conventions][lint_naming]
- [x] Added passing UI tests (including committed `.stderr` file)
- [ ] `cargo test` passes locally
- [x] Executed `./util/dev update_lints`
- [ ] Added lint documentation
- [ ] Run `./util/dev fmt`

[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints

Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.

Delete this line and everything above before opening your PR -->

- [x] suggest replacing `assert!(false, "msg")` with `panic!("msg")`
- [x] extend to allow ~~variables~~ any expression for `"msg"`
- ~~suggest replacing `assert!(false, "msg {}", "arg")` with `panic!("msg {}", "arg")`~~

changelog: add assert message to suggestion in lint assertions_on_constants

Work towards fixing: #3575
@bors
Copy link
Collaborator

bors commented Oct 9, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing d97fbdb to master...

@bors bors merged commit 6ee8d75 into rust-lang:master Oct 9, 2019
bors added a commit that referenced this pull request Oct 11, 2019
Use match_function_call wherever possible

Move `match_function_call` to `utils` and use it wherever possible as discussed in #4635.

changelog: none
bors added a commit that referenced this pull request Oct 12, 2019
Use match_function_call wherever possible

Move `match_function_call` to `utils` and use it wherever possible as discussed in #4635.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants