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

Fix S4158: "Empty Collections Should Not Be Enumerated" should not throw InvalidOperationException #1002

Closed
antrv opened this issue Dec 5, 2017 · 1 comment
Assignees
Labels
Type: Bug Exceptions and blocking issues during analysis.
Milestone

Comments

@antrv
Copy link

antrv commented Dec 5, 2017

Hi,
I updated VS till 15.5 release version and I get the following exception.
I tried 6.4.1.3596 and 6.7.0.4267 versions of SonarAnalyzer.CSharp package.
Thank you.

using System;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        public Program(string id)
        {
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException(nameof(id));
        }
    }
}
Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp8
SyntaxTree: C:\Workplace\Temp\ConsoleApp8\ConsoleApp8\Program.cs
SyntaxNode: Program(string id) { if (id < 1 ... [ConstructorDeclarationSyntax]@[358..498) (15,8)-(20,9)

System.InvalidOperationException: This operation does not apply to an empty instance.
   at System.Collections.Immutable.ImmutableStack`1.Peek()
   at SonarAnalyzer.SymbolicExecution.ProgramState.PopValue(SymbolicValue& poppedValue)
   at SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated.EmptyCollectionAccessedCheck.<>c__DisplayClass15_0.<GetArgumentSymbolicValues>b__0(ArgumentSyntax argument)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated.EmptyCollectionAccessedCheck.RemoveCollectionConstraintsFromArguments(ArgumentListSyntax argumentList, ProgramState programState)
   at SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated.EmptyCollectionAccessedCheck.ProcessInvocation(ProgramState programState, InvocationExpressionSyntax invocation)
   at SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated.EmptyCollectionAccessedCheck.PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
   at SonarAnalyzer.SymbolicExecution.CSharpExplodedGraph.<>c__DisplayClass3_0.<VisitInstruction>b__0(ProgramState ps, ExplodedGraphCheck check)
   at SonarAnalyzer.SymbolicExecution.CSharpExplodedGraph.<>c__DisplayClass4_0.<InvokeChecks>b__0(ProgramState ps, ExplodedGraphCheck check)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at SonarAnalyzer.SymbolicExecution.CSharpExplodedGraph.InvokeChecks(ProgramState programState, Func`3 invoke)
   at SonarAnalyzer.SymbolicExecution.CSharpExplodedGraph.VisitInstruction(ExplodedGraphNode node)
   at SonarAnalyzer.SymbolicExecution.AbstractExplodedGraph.Walk()
   at SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated.CheckForEmptyCollectionAccess(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.SymbolicExecution.FlowAnalysisExtensions.Analyze(CSharpSyntaxNode declarationBody, ISymbol symbol, Action`2 analyze, SyntaxNodeAnalysisContext context)
   at SonarAnalyzer.SymbolicExecution.FlowAnalysisExtensions.<>c__DisplayClass0_0.<RegisterExplodedGraphBasedAnalysis>b__0(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.DiagnosticAnalyzerContextHelper.<>c__DisplayClass0_0`1.<RegisterSyntaxNodeActionInNonGenerated>b__0(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.SonarAnalysisContext.<>c__DisplayClass16_0`1.<RegisterSyntaxNodeAction>b__0(SyntaxNodeAnalysisContext c)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__43`1.<ExecuteSyntaxNodeAction>b__43_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

@Evangelink Evangelink added Area: Rules Type: Bug Exceptions and blocking issues during analysis. labels Dec 12, 2017
@Evangelink
Copy link
Contributor

Hi @antrv,

Thanks for the feedback. I have managed to reproduce the issue using your code sample. We will fix this bug ASAP.

@Evangelink Evangelink added this to the 6.7.1 milestone Dec 18, 2017
@Evangelink Evangelink self-assigned this Dec 18, 2017
@Evangelink Evangelink changed the title InvalidOperationException in EmptyCollectionsShouldNotBeEnumerated [VS 15.5 only] Fix EmptyCollectionsShouldNotBeEnumerated: Rule should not throw InvalidOperationException Dec 18, 2017
@ghost ghost added the Status: Needs Review label Dec 18, 2017
@ghost ghost removed the Status: Needs Review label Dec 18, 2017
@valhristov valhristov changed the title Fix EmptyCollectionsShouldNotBeEnumerated: Rule should not throw InvalidOperationException Fix S4158: "Empty Collections Should Not Be Enumerated" should not throw InvalidOperationException Dec 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Exceptions and blocking issues during analysis.
Projects
None yet
Development

No branches or pull requests

2 participants