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

feat: add trait_associated_const_default_removed lint #888

Conversation

mrnossiom
Copy link
Contributor

Lint for "non-sealed trait removed the default value for an associated constant"

See #870

I still haven't fully made myself of a mental model of how to create queries. Here's a starting point. The query I made matches another test with incoherent results.

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

The query is very close! I've added an explanation for what is going wrong and why, hopefully that makes the problem clear.

Thanks for putting this together!

Comment on lines 44 to 47
associated_constant @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
name @filter(op: "=", value: ["%associated_constant"])
default @filter(op: "is_not_null")
}
Copy link
Owner

Choose a reason for hiding this comment

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

This clause says: "there are no associated constants such that both the name matches and there is a default value." So not having any associated constant at all is a match, as is having only constants with different names, or only having constants without default values.

We want something different: "there exists an associated constant with a matching name and with a default value." This way, not having the right constant name is not a match, and not having a default value is not a match.

The clause we want is actually the inverted version of what the query currently says. The query says "there does not exist" but we actually do want it to exist. Merely removing the "there does not exist" part of the query (the "count is zero" part) is sufficient to fix it:

Suggested change
associated_constant @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
name @filter(op: "=", value: ["%associated_constant"])
default @filter(op: "is_not_null")
}
associated_constant {
name @filter(op: "=", value: ["%associated_constant"])
default @filter(op: "is_not_null")
}

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Excellent! 🚀

@obi1kenobi obi1kenobi merged commit 0f91c74 into obi1kenobi:main Aug 27, 2024
33 checks passed
@obi1kenobi
Copy link
Owner

Did my explanation of the query change make sense btw? Happy to answer any further questions you may have!

@mrnossiom mrnossiom deleted the add-trait-associated-const-default-removed branch August 27, 2024 13:11
@mrnossiom
Copy link
Contributor Author

The explanation makes perfect sense once you have it, but to reach this myself, I think I need a bit more practice with CrateDiff queries and trustfall in general.

@obi1kenobi
Copy link
Owner

Of course, makes sense. Happy to support you as you practice.

Btw I just made a Discord server for cargo-semver-checks and Trustfall, to make coordination a bit easier. You're welcome to join if you'd like — there are many other people in similar places in their learning journey there: https://discord.gg/s7Cfmp3P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants