-
Notifications
You must be signed in to change notification settings - Fork 73
Rule to recommend if-else construct instead of match clause #822
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
Open
webwarrior-ws
wants to merge
6
commits into
fsprojects:master
Choose a base branch
from
webwarrior-ws:favourIfElseForBinaryCondition-rebase
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef5d151
Rule to recommend if-else construct instead of match clause
janus 0f513a5
FavourBasicControlFlow: rename to RecommendIfElseConstructOverMatch
webwarrior-ws fd9c67d
RecommendIfElseConstructOverMatch: added test
webwarrior-ws d703c99
RecommendIfElseConstructOverMatch: fix rule
webwarrior-ws 0ae8f45
RecommendIfElseConstructOverMatch: added 2 more tests
webwarrior-ws efa0362
RecommendIfElseConstructOverMatch: fix rule
webwarrior-ws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| title: FL0092 | ||
| category: how-to | ||
| hide_menu: true | ||
| --- | ||
|
|
||
| # RecommendIfElseConstructOverMatch (FL0092) | ||
|
|
||
| *Introduced in `0.26.12`* | ||
|
|
||
| ## Cause | ||
|
|
||
| Use of match clause when it is used in basic control flow. | ||
|
|
||
| ## Rationale | ||
|
|
||
| Match clauses are more adequate for complex pattern-matching scenarios. | ||
| For simple comparisons, an if-then-else block makes terser code. | ||
|
|
||
| ## How To Fix | ||
|
|
||
| Use if-else construct instead of match clause. | ||
|
|
||
| ## Rule Settings | ||
|
|
||
| { | ||
| "recommendIfElseConstructOverMatch": { | ||
| "enabled": false | ||
| } | ||
| } |
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
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
38 changes: 38 additions & 0 deletions
38
src/FSharpLint.Core/Rules/Conventions/RecommendIfElseConstructOverMatch.fs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module FSharpLint.Rules.RecommendIfElseConstructOverMatch | ||
|
|
||
| open FSharpLint.Framework | ||
| open FSharpLint.Framework.Suggestion | ||
| open FSharp.Compiler.Syntax | ||
| open FSharp.Compiler.Text | ||
| open FSharpLint.Framework.Ast | ||
| open FSharpLint.Framework.Rules | ||
|
|
||
| let runner args = | ||
| let isBasicControlFlow (synMatchClauses: List<SynMatchClause>) = | ||
| let (|Wildcard|_|) clause = | ||
| match clause with | ||
| | SynMatchClause(SynPat.Wild _, None, _, _, _, _) -> true | ||
| | _ -> false | ||
|
|
||
| match synMatchClauses with | ||
| | [ SynMatchClause(SynPat.Null(_), _, _, _, _, _); Wildcard ] | ||
| | [ SynMatchClause(SynPat.Const(_), _, _, _, _, _); Wildcard ] | ||
| | [ SynMatchClause(SynPat.Named(SynIdent.SynIdent(_), _, _, _), None, _, _, _, _); Wildcard ] | ||
| | [ SynMatchClause(SynPat.LongIdent(SynLongIdent(_), _, _, SynArgPats.Pats [], _, _), _, _, _, _, _); Wildcard ] -> | ||
| true | ||
| | _ -> false | ||
|
|
||
| match args.AstNode with | ||
| | AstNode.Expression(SynExpr.Match (_, _, synMatchClauses, range, _)) when isBasicControlFlow synMatchClauses -> | ||
| { Range = range | ||
| Message = Resources.GetString "RecommendIfElseConstructOverMatch" | ||
| SuggestedFix = None | ||
| TypeChecks = List.empty } | ||
| |> Array.singleton | ||
| | _ -> Array.empty | ||
|
|
||
| let rule = | ||
| { Name = "RecommendIfElseConstructOverMatch" | ||
| Identifier = Identifiers.RecommendIfElseConstructOverMatch | ||
| RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = ignore } } | ||
| |> AstNodeRule |
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
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
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
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
75 changes: 75 additions & 0 deletions
75
tests/FSharpLint.Core.Tests/Rules/Conventions/RecommendIfElseConstructOverMatch.fs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| module FSharpLint.Core.Tests.Rules.Conventions.RecommendIfElseConstructOverMatch | ||
|
|
||
| open NUnit.Framework | ||
| open FSharpLint.Rules | ||
| open FSharpLint.Core.Tests | ||
|
|
||
| [<TestFixture>] | ||
| type TestConventionsRecommendIfElseConstructOverMatch() = | ||
| inherit TestAstNodeRuleBase.TestAstNodeRuleBase(RecommendIfElseConstructOverMatch.rule) | ||
|
|
||
|
|
||
| [<Test>] | ||
| member this.MoreThanTwoClausesShouldNotProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | bar -> () | ||
| | baz -> () | ||
| | _ -> () """ | ||
|
|
||
| Assert.IsTrue this.NoErrorsExist | ||
|
|
||
|
|
||
| [<Test>] | ||
| member this.TwoClausesWithoutWildcardShouldNotProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | bar -> () | ||
| | baz -> () """ | ||
|
|
||
| Assert.IsTrue this.NoErrorsExist | ||
|
|
||
| [<Test>] | ||
| member this.ThePresenceOfDUShouldNotProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | Bar baz -> () | ||
| | _ -> () """ | ||
|
|
||
| Assert.IsTrue this.NoErrorsExist | ||
|
|
||
| [<Test>] | ||
| member this.ThePresenceOfWhenClauseShouldNotProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | bar when bar.Count = 0 -> () | ||
| | _ -> ()""" | ||
|
|
||
| Assert.IsTrue this.NoErrorsExist | ||
|
|
||
| [<Test>] | ||
| member this.TwoClausesWithWildcardShouldProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | bar -> () | ||
| | _ -> () """ | ||
|
|
||
| Assert.IsTrue this.ErrorsExist | ||
|
|
||
| [<Test>] | ||
| member this.TwoClausesWithNullAndWildcardShouldProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | null -> () | ||
| | _ -> () """ | ||
|
|
||
| Assert.IsTrue this.ErrorsExist | ||
|
|
||
| [<Test>] | ||
| member this.TwoClausesWithNullAndVariableShouldNotProduceError() = | ||
| this.Parse """ | ||
| match foo with | ||
| | null -> () | ||
| | notNullFoo -> () """ | ||
|
|
||
| Assert.IsTrue this.NoErrorsExist | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webwarrior-ws this cannot be written as
if foo = Baz.Bar?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it can. Though in this situation pattern matching seems more natural choice.
So should I remove this case from exceptions to the rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes let's remove it because I want to see what's the impact on SelfCheck; keep your change around in case we put it back