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 S3928: Rule throws NullReferenceException #792

Closed
antrv opened this issue Sep 21, 2017 · 2 comments
Closed

Fix S3928: Rule throws NullReferenceException #792

antrv opened this issue Sep 21, 2017 · 2 comments
Assignees
Labels
Type: Bug Exceptions and blocking issues during analysis.
Milestone

Comments

@antrv
Copy link

antrv commented Sep 21, 2017

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public void Method(IEnumerable<string> items)
        {
            foreach (var item in items)
            {
                switch (item)
                {
                    case "a":
                    {
                        break;
                    }

                    default:
                        throw new ArgumentOutOfRangeException(nameof(item), item, null);
                }
            }
        }
    }
}

When compiling the following simple console application, SonarAnalyzer.CSharp gives the exception:

AD0001	Analyzer 'SonarAnalyzer.Rules.CSharp.CheckArgumentException' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.	ConsoleApp2		1	Active	Analyzer 'SonarAnalyzer.Rules.CSharp.CheckArgumentException' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp2
SyntaxTree: C:\Users\anton.ryzhov\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2\Program.cs
SyntaxNode: new ArgumentOutOfRangeException ... [ObjectCreationExpressionSyntax]@[384..441) (18,22)-(18,79)
System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Rules.CSharp.CheckArgumentException.TakeOnlyBeforeDot(Optional`1 value)
   at SonarAnalyzer.Rules.CSharp.CheckArgumentException.CheckForIssue(SyntaxNodeAnalysisContext analysisContext)
   at SonarAnalyzer.Helpers.DiagnosticAnalyzerContextHelper.<>c__DisplayClass0_0`1.<RegisterSyntaxNodeActionInNonGenerated>b__0(SyntaxNodeAnalysisContext c)
   at SonarAnalyzer.Helpers.SonarAnalysisContext.<>c__DisplayClass12_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)

EDIT: I have changed the styling (@Evangelink)

@antrv antrv changed the title Exception in SonarAnalyzer.CSharp CheckArgumentException in SonarAnalyzer.CSharp Sep 21, 2017
@antrv
Copy link
Author

antrv commented Sep 21, 2017

Sorry for styling :)

@antrv antrv changed the title CheckArgumentException in SonarAnalyzer.CSharp NullReferenceException in CheckArgumentException Sep 21, 2017
@michalb-sonar
Copy link
Contributor

Thanks for the report and good repro code @antrv. I was able to reproduce the problem, we'll fix it soon.

@michalb-sonar michalb-sonar changed the title NullReferenceException in CheckArgumentException Fix S3928: Rule throws NullReferenceException Sep 21, 2017
@michalb-sonar michalb-sonar added Area: Rules Type: Bug Exceptions and blocking issues during analysis. labels Sep 21, 2017
@michalb-sonar michalb-sonar added this to the 6.5 milestone Sep 21, 2017
@michalb-sonar michalb-sonar self-assigned this Oct 4, 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