@@ -37,35 +37,48 @@ public IEnumerable<AnalysisResult> Analyze(SyntaxTree syntaxTree, SemanticModel
3737 . Where ( replaceabilityInfo => replaceabilityInfo . switchStatement != null )
3838 . Select ( replaceabilityInfo => new AnalysisResult
3939 (
40- replaceabilityInfo . category == SwitchStatementSectionsCategory . AllSwitchSectionsAreReturnStatements
41- ? ( ISharpenSuggestion ) ReplaceSwitchStatementContainingOnlyReturnsWithSwitchExpression . Instance
42- : ReplaceSwitchStatementContainingOnlyAssignmentsWithSwitchExpression . Instance ,
40+ GetSuggestion ( replaceabilityInfo . category , replaceabilityInfo . isSurelyExhaustive ) ,
4341 analysisContext ,
4442 syntaxTree . FilePath ,
4543 replaceabilityInfo . switchStatement . SwitchKeyword ,
4644 replaceabilityInfo . switchStatement
4745 ) ) ;
4846
49- ( SwitchStatementSyntax switchStatement , SwitchStatementSectionsCategory category ) GetSwitchStatementPotentialReplaceabilityInfo ( SwitchStatementSyntax switchStatement )
47+ ISharpenSuggestion GetSuggestion ( SwitchStatementSectionsCategory category , bool isSurelyExhaustive )
48+ {
49+ // TODO-IG: It will be so nice to switch to switch statement one day we move Sharpen to C# 8.0 :-)
50+ if ( category == SwitchStatementSectionsCategory . AllSwitchSectionsAreAssignmentsToTheSameIdentifier )
51+ {
52+ return isSurelyExhaustive
53+ ? ( ISharpenSuggestion ) ReplaceSwitchStatementContainingOnlyAssignmentsWithSwitchExpression . Instance
54+ : ConsiderReplacingSwitchStatementContainingOnlyAssignmentsWithSwitchExpression . Instance ;
55+ }
56+ else
57+ {
58+ return isSurelyExhaustive
59+ ? ( ISharpenSuggestion ) ReplaceSwitchStatementContainingOnlyReturnsWithSwitchExpression . Instance
60+ : ConsiderReplacingSwitchStatementContainingOnlyReturnsWithSwitchExpression . Instance ;
61+ }
62+ }
63+
64+ ( SwitchStatementSyntax switchStatement , SwitchStatementSectionsCategory category , bool isSurelyExhaustive ) GetSwitchStatementPotentialReplaceabilityInfo ( SwitchStatementSyntax switchStatement )
5065 {
5166 // We have to have at least one switch section (case or default).
52- if ( switchStatement . Sections . Count <= 0 ) return ( null , SwitchStatementSectionsCategory . None ) ;
67+ if ( switchStatement . Sections . Count <= 0 ) return ( null , SwitchStatementSectionsCategory . None , false ) ;
5368
54- // We have to have the default section, otherwise we cannot replace the switch
55- // statement with a switch expressions.
56- // TODO-IG: Provide additional "consider" suggestion in case of missing the return.
57- // Something like "Consider adding the default case and replacing switch statement with switch expression".
58- if ( ! switchStatement . Sections . Any ( switchSection =>
59- switchSection . Labels . Any ( label => label . IsKind ( SyntaxKind . DefaultSwitchLabel ) ) ) )
60- return ( null , SwitchStatementSectionsCategory . None ) ;
69+ // If we have the default section it is surely exhaustive.
70+ // Otherwise we cannot be sure. We will of course not do any
71+ // proper check that the compiler does.
72+ bool isSurelyExhaustive = switchStatement . Sections . Any ( switchSection =>
73+ switchSection . Labels . Any ( label => label . IsKind ( SyntaxKind . DefaultSwitchLabel ) ) ) ;
6174
6275 if ( AllSwitchSectionsAreAssignmentsToTheSameIdentifier ( switchStatement . Sections ) )
63- return ( switchStatement , SwitchStatementSectionsCategory . AllSwitchSectionsAreAssignmentsToTheSameIdentifier ) ;
76+ return ( switchStatement , SwitchStatementSectionsCategory . AllSwitchSectionsAreAssignmentsToTheSameIdentifier , isSurelyExhaustive ) ;
6477
6578 if ( AllSwitchSectionsAreReturnStatements ( switchStatement . Sections ) )
66- return ( switchStatement , SwitchStatementSectionsCategory . AllSwitchSectionsAreReturnStatements ) ;
79+ return ( switchStatement , SwitchStatementSectionsCategory . AllSwitchSectionsAreReturnStatements , isSurelyExhaustive ) ;
6780
68- return ( null , SwitchStatementSectionsCategory . None ) ;
81+ return ( null , SwitchStatementSectionsCategory . None , false ) ;
6982
7083 bool AllSwitchSectionsAreAssignmentsToTheSameIdentifier ( SyntaxList < SwitchSectionSyntax > switchSections )
7184 {
0 commit comments