-
Notifications
You must be signed in to change notification settings - Fork 38
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
Lint/UselessAssign
reports macros accepting Crystal::Macros::TypeDeclaration
#447
Comments
Lint/DebugCalls
on macro invocations that accept Crystal::Macros::TypeDeclaration
Lint/UselessAssign
reports macros accepting Crystal::Macros::TypeDeclaration
This is one of the cases that IMO cannot be fixed at the level of analysis that ameba is doing. You can either use inline pragma, exclude the file on the project level or turn on the I was thinking of |
I think there is indeed some room for improvement, even without semantic analysis. For example, the rule could take into account if the assignment / type declaration is located in a call argument. That would be a strong indicator for a macro call. Using a local var assignment or even a type declaration in an argument would be quite unusual and certainly not well-formed code. Also I'm still wondering why |
It just feels less than ideal that new rules keep being added that cannot be actually robust without semantic analysis, or are more subjective in general. Which ends up forcing me to either disable them inline, globally, or conform to the suggestions on every minor release. I still think some/most of these should be disabled by default, or made optional via some sort of opinionated style extension. |
@straight-shoota Excluding call arguments might work, let's try that 👍 |
@straight-shoota Here you go!
ameba/src/ameba/ast/visitors/scope_visitor.cr Lines 178 to 179 in 28fafea
Just FTR, note that the example is different from the one posted in #429 (comment). @Blacksmoke16 These are definitely topics up for a discussion. Personally, I've had problem with the |
@straight-shoota nope, they're still needed (to handle |
@Sija I think this needs to be reopened, doesn't seem resolved from what I can tell: $ git rev-parse HEAD
590640b559875aa9bb76783be95126dd07d9ed27
$ make
shards build -Dpreview_mt
Dependencies are satisfied
Building: ameba
$ cat test.cr
macro test(type_decl)
def {{type_decl.var.id}} : {{type_decl.type}}
"foo"
end
end
test name : String
$ ./bin/ameba test.cr
Inspecting 1 file
F
test.cr:7:6
[W] Lint/UselessAssign: Useless assignment to variable `name`
> test name : String
^--^
Finished in 21.03 milliseconds
1 inspected, 1 failure |
@Blacksmoke16 you're right, seems that my naive heuristics broke: ameba/src/ameba/rule/lint/useless_assign.cr Lines 57 to 60 in 590640b
|
Proper fix would most likely have to touch the |
The other week I looked into recognition of I solved this by declaring a property # in rule/xyz.cr
# Returns `true` if the visitor is currently inside a macro expression.
@[YAML::Field(ignore: true)]
property? in_macro_expression : Bool = false # in src/ameba/ast/visitors/node_visitor.cr
def visit(node : Crystal::MacroExpression | Crystal::MacroIf | Crystal::MacroFor)
rule = @rule
if rule.responds_to?(:in_macro_expression=)
rule.in_macro_expression = true
end
!skip?(node)
end
def end_visit(node : Crystal::MacroExpression | Crystal::MacroIf | Crystal::MacroFor)
rule = @rule
if rule.responds_to?(:in_macro_expression=)
rule.in_macro_expression = false
end
end I figure this could work pretty similarly for |
Avram models uses macros like these to declare the database columns, so the very useful |
@hugopl That's a bummer indeed. OTOH IIUC the |
I didn't know about this option, I'll give it a try, thanks. Was this option introduced with the new behavior? Or was it always there? |
Answering to my own question, yes, this options was introduced on 1.6.1. CI here runs an older version and I got:
But yes, it solved the warnings with Avran, OTOH it loses the ability to detect unused variables declared like: a : Int32? = nil |
Just adding to this since I just upgraded Ameba on Lucky. The entire Lucky ecosystem gets hit by it since all Habitat configs, params, and use of |
This should probably not be flagged since Ameba can't know what the custom macro is doing.
The text was updated successfully, but these errors were encountered: