Don't report SC2218 when another definition's order is unknown - #3520
Open
Eljees wants to merge 1 commit into
Open
Don't report SC2218 when another definition's order is unknown#3520Eljees wants to merge 1 commit into
Eljees wants to merge 1 commit into
Conversation
checkUseBeforeDefinition warned whenever any definition of a function post-dominated the call. With two definitions -- one inside a function body that nothing visibly invokes, one at top level after the call -- the second satisfied that test while the first was ignored, so a call that may well have been preceded by a definition got reported as being "only defined later". That is the shape bats produces: setup() defines helpers, often by sourcing a library, one @test calls a helper, and a later, unrelated @test re-sources the same library. Requiring every definition to come after the call keeps the true positives and drops this one. A definition whose order we cannot determine may already have run -- via bats, a trap, an indirect call, or a sourcing parent -- which is why SC2329 is only an info. Fixes koalaman#3509
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3509.
Minimal reproduction
The report says the case could not be shrunk below the 1069-line real library. It does reduce, and neither
sourcenor bats is needed:The reported shape, with nothing sourced:
Cause
checkUseBeforeDefinitionwarns when some definition post-dominates the call and no definition is post-dominated by it. Here there are two definitions: one insidesetup's body, one at top level after the call. The top-level one satisfies the first guard; the one insetupsatisfies neither relation, because nothing in the script visibly callssetup. So the check concludes "only defined later" while a definition whose order it cannot determine is sitting right there.That definition may well have run. bats calls
setupbefore every@test, and in an ordinary script a function can be reached through a trap, an indirect call, or a sourcing parent. SC2218 is an error, whereas SC2329 ("This function is never invoked") is only an info, hedged with "or ignored if invoked indirectly", for exactly that reason.Change
Require every definition to come after the call rather than just one. The second guard stays as it is: inside a loop a definition can both post-dominate the call and be post-dominated by it.
The true positives are unaffected:
prop_checkUseBeforeDefinition1and5still fire,2,3,4and6still don't. Two props are added for the two shapes above. Both fail on master and pass with the change, andcabal testis green.The reporter's original case, with their
betteropts.sh, goes from the SC2218 above to exit 0.