Add NestedWith check - #1306
Open
easink wants to merge 1 commit into
Open
Conversation
Reports a `with` that does not start at the top of a function body: one buried inside an `if`, `unless`, `case`, `cond`, `for`, `receive`, or another `with`. Such a `with` splits a chain of pattern matches across two levels of control flow, which hides its failure paths and makes the `else` of either block ambiguous to a reader. `def`, `defp`, `defmacro`, `defmacrop`, `fn` and `defmodule` open a scope, so a `with` at the top of one of their bodies is never reported, however deeply the scope itself is nested. `defmodule` counts because a module is routinely wrapped in a compile-time `if Code.ensure_loaded?(Dep)`, which is conditional compilation rather than control flow around the `with`. `try` is neither scope nor block and is passed through: wrapping a chain in `try/rescue` does not branch it, and a `try` has no `else` to confuse with the one belonging to the `with`. The traversal is an explicit recursion rather than `Credo.Code.prewalk/3` because the flag it carries has to be reset per branch, and reset entirely on the way into a scope. Opt-in for now, so it lands in `:disabled`.
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.
Reports a
withthat does not start at the top of a function body: one buried inside anif,unless,case,cond,for,receive, or anotherwith. Such awithsplits a chain of pattern matches across two levels of control flow, which hides its failure paths and makes theelseof either block ambiguous to a reader.def,defp,defmacro,defmacrop,fnanddefmoduleopen a scope, so awithat the top of one of their bodies is never reported, however deeply the scope itself is nested.defmodulecounts because a module is routinely wrapped in a compile-timeif Code.ensure_loaded?(Dep), which is conditional compilation rather than control flow around thewith.tryis neither scope nor block and is passed through: wrapping a chain intry/rescuedoes not branch it, and atryhas noelseto confuse with the one belonging to thewith.The traversal is an explicit recursion rather than
Credo.Code.prewalk/3because the flag it carries has to be reset per branch, and reset entirely on the way into a scope.Opt-in for now, so it lands in
:disabled.