Skip to content

Commit ad0aa48

Browse files
committed
Fix bug in Looped Random Instantiation when object is the type.
1 parent e8dd227 commit ad0aa48

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

VSDiagnostics/VSDiagnostics/VSDiagnostics.Test/Tests/General/LoopedRandomInstantiationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,23 @@ void Method()
194194

195195
VerifyDiagnostic(original);
196196
}
197+
198+
[TestMethod]
199+
public void LoopedRandomInstantiation_TypeIsObject_DoesNotCrashAnalyzerBecauseContainingNamespaceIsNull()
200+
{
201+
var original = @"
202+
namespace ConsoleApplication1
203+
{
204+
class MyClass
205+
{
206+
void Method()
207+
{
208+
object[] o = {};
209+
}
210+
}
211+
}";
212+
213+
VerifyDiagnostic(original);
214+
}
197215
}
198216
}

VSDiagnostics/VSDiagnostics/VSDiagnostics/Diagnostics/General/LoopedRandomInstantiation/LoopedRandomInstantiationAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ private void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
3636
if (type == null) { return; }
3737

3838
var typeInfo = context.SemanticModel.GetTypeInfo(type).Type;
39-
if (typeInfo == null) { return; }
4039

41-
if (typeInfo.OriginalDefinition.ContainingNamespace.Name != nameof(System) ||
40+
if (typeInfo?.OriginalDefinition.ContainingNamespace == null ||
41+
typeInfo.OriginalDefinition.ContainingNamespace.Name != nameof(System) ||
4242
typeInfo.Name != nameof(System.Random))
4343
{
4444
return;

0 commit comments

Comments
 (0)