Skip to content

Commit

Permalink
Fix RCS1250 (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 17, 2024
1 parent 168893c commit 248e785
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS0049](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0049) ([PR](https://github.com/dotnet/roslynator/pull/1386))
- Fix analyzer [RCS1159](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1159) ([PR](https://github.com/dotnet/roslynator/pull/1390))
- Fix analyzer [RCS1019](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1019) ([PR](https://github.com/dotnet/roslynator/pull/1402))
- Fix analyzer [RCS1250](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1250) ([PR](https://github.com/dotnet/roslynator/pull/1403))
- Fix code fix for [CS8600](https://josefpihrt.github.io/docs/roslynator/fixes/CS8600) changing the wrong type when casts or `var` are involved ([PR](https://github.com/dotnet/roslynator/pull/1393) by @jroessel)

## [4.10.0] - 2024-01-24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.CodeStyle;
Expand Down Expand Up @@ -48,7 +50,10 @@ protected override void ReportVarToExplicit(ref SyntaxNodeAnalysisContext contex
#if ROSLYN_4_7
protected override bool UseCollectionExpressionFromImplicit(ref SyntaxNodeAnalysisContext context)
{
return ((ImplicitObjectCreationExpressionSyntax)context.Node).ArgumentList?.Arguments.Any() != true
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntax)context.Node;

return implicitObjectCreation.ArgumentList?.Arguments.Any() != true
&& implicitObjectCreation.Initializer?.Expressions.Any(f => f.IsKind(SyntaxKind.SimpleAssignmentExpression)) != true
&& UseCollectionExpression(ref context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1854,4 +1854,31 @@ void M(List<string> x)
}
", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Explicit));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
public async Task TestNoDiagnostic_ObjectInitializerWithPropertySet()
{
await VerifyNoDiagnosticAsync("""
using System;
using System.Collections;
using System.Collections.Generic;

class C : IEnumerable<int>
{
public string P { get; set; }

public IEnumerator<int> GetEnumerator() => throw new NotImplementedException();

IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();

static C M()
{
C c = new() { P = "" };

return c;
}
}
""", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Implicit)
.AddConfigOption(ConfigOptionKeys.UseCollectionExpression, true));
}
}

0 comments on commit 248e785

Please sign in to comment.