Skip to content

Commit abddc87

Browse files
committed
Addressed CR feedback
1 parent 08fe6d1 commit abddc87

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1129UnitTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void TestMethod()
6262
var v1 = new TestStruct(1);
6363
var v2 = new TestStruct(1) { TestProperty = 2 };
6464
var v3 = new TestStruct() { TestProperty = 2 };
65+
var v4 = new TestStruct { TestProperty = 2 };
6566
}
6667
6768
private struct TestStruct
@@ -183,6 +184,55 @@ private struct TestStruct
183184
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
184185
}
185186

187+
/// <summary>
188+
/// Verifies that new expressions for value types through generics will generate diagnostics.
189+
/// </summary>
190+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
191+
[Fact]
192+
public async Task VerifyGenericsTypeCreationAsync()
193+
{
194+
var testCode = @"public class TestClass
195+
{
196+
public T TestMethod1<T>()
197+
where T : struct
198+
{
199+
return new T();
200+
}
201+
202+
public T TestMethod2<T>()
203+
where T : new()
204+
{
205+
return new T();
206+
}
207+
}
208+
";
209+
210+
var fixedTestCode = @"public class TestClass
211+
{
212+
public T TestMethod1<T>()
213+
where T : struct
214+
{
215+
return default(T);
216+
}
217+
218+
public T TestMethod2<T>()
219+
where T : new()
220+
{
221+
return new T();
222+
}
223+
}
224+
";
225+
226+
DiagnosticResult[] expected =
227+
{
228+
this.CSharpDiagnostic().WithLocation(6, 16)
229+
};
230+
231+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
232+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
233+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
234+
}
235+
186236
/// <inheritdoc/>
187237
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
188238
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1129CodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override FixAllProvider GetFixAllProvider()
3333

3434
public override Task RegisterCodeFixesAsync(CodeFixContext context)
3535
{
36-
foreach (var diagnostic in context.Diagnostics.Where(d => this.FixableDiagnosticIds.Contains(d.Id)))
36+
foreach (var diagnostic in context.Diagnostics)
3737
{
3838
context.RegisterCodeFix(
3939
CodeAction.Create(

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1129DoNotUseDefaultValueTypeConstructor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.ReadabilityRules
55
{
6+
using System;
67
using System.Collections.Immutable;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp;
@@ -47,12 +48,12 @@ private static void HandleNewExpression(SyntaxNodeAnalysisContext context)
4748
ObjectCreationExpressionSyntax newExpression = (ObjectCreationExpressionSyntax)context.Node;
4849

4950
var typeToCreate = context.SemanticModel.GetTypeInfo(newExpression, context.CancellationToken);
50-
if (typeToCreate.Type.IsReferenceType)
51+
if (typeToCreate.Type.IsReferenceType || IsReferenceTypeParameter(typeToCreate.Type))
5152
{
5253
return;
5354
}
5455

55-
if (newExpression.ArgumentList.Arguments.Count > 0)
56+
if ((newExpression.ArgumentList == null) || (newExpression.ArgumentList.Arguments.Count > 0))
5657
{
5758
return;
5859
}
@@ -64,5 +65,10 @@ private static void HandleNewExpression(SyntaxNodeAnalysisContext context)
6465

6566
context.ReportDiagnostic(Diagnostic.Create(Descriptor, newExpression.GetLocation()));
6667
}
68+
69+
private static bool IsReferenceTypeParameter(ITypeSymbol type)
70+
{
71+
return (type.Kind == SymbolKind.TypeParameter) && !((ITypeParameterSymbol)type).HasValueTypeConstraint;
72+
}
6773
}
6874
}

0 commit comments

Comments
 (0)