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

LoopOverLength check #338

Merged
merged 11 commits into from
May 24, 2022
Merged

LoopOverLength check #338

merged 11 commits into from
May 24, 2022

Conversation

pfitzseb
Copy link
Member

Fixes #337.

@non-Jedi
Copy link
Member

Does check_loop_iter get called on comprehensions or only for loops?

@pfitzseb
Copy link
Member Author

Ah, good call. Generators and for loops with multiple iterators now work:
image

@fredrikekre
Copy link
Member

Shouldn't it check that the index is used for indexing? There is nothing wrong with having a loop from 1 to the length of something.

@pfitzseb
Copy link
Member Author

Yes.
image

@pfitzseb pfitzseb requested a review from fredrikekre May 17, 2022 13:43
@pfitzseb
Copy link
Member Author

Also fixed the wording:
image

@non-Jedi
Copy link
Member

There's a bikeshedding discussion to be hadavoided on whether the linter should highlight the for-loop or the indexing location.

Thanks so much for tackling this @pfitzseb. Hopefully this is one of those little changes that nudge the whole ecosystem towards more care for correctness.

@pfitzseb
Copy link
Member Author

pfitzseb commented May 17, 2022

I was thinking about this too, which is why both are highlighted now (turns out I never committed those changes):
image

@pfitzseb pfitzseb requested a review from davidanthoff May 17, 2022 16:50
fredrikekre
fredrikekre previously approved these changes May 18, 2022
Copy link
Member

@fredrikekre fredrikekre left a comment

Choose a reason for hiding this comment

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

LGTM but don't know this code that well.

@pfitzseb
Copy link
Member Author

pfitzseb commented May 18, 2022

image

The check is now disabled if we infer the array to be a Base.Array, for which this indexing method is valid.

@Seelengrab
Copy link

This looks great! Could this be extended to multidimensional arrays and encouraging axes, if that's not already a thing that I just missed?

@pfitzseb
Copy link
Member Author

I've adjusted the message:
image

Currently only Base.length and Base.size are handled.

@fredrikekre
Copy link
Member

I got a crash with:

ERROR: type Nothing has no field type
Stacktrace:
 [1] getproperty(x::Nothing, f::Symbol)
   @ Base ./Base.jl:42
 [2] check_incorrect_iter_spec(x::CSTParser.EXPR, body::CSTParser.EXPR, env::StaticLint.ExternalEnv)
   @ StaticLint ~/.julia/packages/StaticLint/ktd1a/src/linting/checks.jl:388
 [3] check_loop_iter(x::CSTParser.EXPR, env::StaticLint.ExternalEnv)
   @ StaticLint ~/.julia/packages/StaticLint/ktd1a/src/linting/checks.jl:357

@pfitzseb
Copy link
Member Author

Should be fixed now.

@pfitzseb pfitzseb merged commit 556bb79 into master May 24, 2022
@davidanthoff davidanthoff deleted the sp/loop-over-length-check branch May 24, 2022 16:27
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.

Add lint for for i=1:length(v)
4 participants