Skip to content

Conversation

gerhardol
Copy link
Member

Depends on #11240
See also #11371

Proposed changes

Use collection expressions: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions

Basically, it is an enhanced new(), like:

-            List<BatchArgumentItem> batches = new();
+            List<BatchArgumentItem> batches = [];

(This PR enables that syntax also where new() not applied previously.)

Also some conditional allowed (but I had to remove some added by the analyzer due to syntax failure).
Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'collection expressions' and 'collection expressions' UI.IntegrationTests C:\dev\gc\gitextensions\IntegrationTests\UI.IntegrationTests\GitUICommands\RunCommandTests.cs 212 Active

Not working

-            List<string> args = new() { "ge.exe", command, "filename" };
-            args.Add(commitValid ? _referenceRepository.CommitHash : "no-commit");
-            if (filter)
-            {
-                args.Add(filterValid ? "--filter-by-revision" : "invalid");
-            }
+            List<string> args =
+            [
+                "ge.exe", command, "filename",
+                commitValid ? _referenceRepository.CommitHash : "no-commit",
+                .. filter ? [filterValid ? "--filter-by-revision" : "invalid"] : [],
+            ];

OK

diff --git a/GitCommands/Git/Commands.Arguments.cs b/GitCommands/Git/Commands.Arguments.cs
index baff60..51f18 100644
--- a/GitCommands/Git/Commands.Arguments.cs
+++ b/GitCommands/Git/Commands.Arguments.cs
@@ -351,10 +351,12 @@ static ArgumentString GitRefsPattern(RefsFilter option)
                     return string.Empty;
                 }
 
-                ArgumentBuilder builder = new();
-                builder.Add((option & RefsFilter.Heads) != 0, "refs/heads/");
-                builder.Add((option & RefsFilter.Remotes) != 0, "refs/remotes/");
-                builder.Add((option & RefsFilter.Tags) != 0, "refs/tags/");
+                ArgumentBuilder builder = new()
+                {
+                    { (option & RefsFilter.Heads) != 0, "refs/heads/" },
+                    { (option & RefsFilter.Remotes) != 0, "refs/remotes/" },
+                    { (option & RefsFilter.Tags) != 0, "refs/tags/" }
+                };
 
                 return builder;
             }

Note that stylecop does not allow this syntax yet:
DotNetAnalyzers/StyleCopAnalyzers#3687
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md

newline hade to be added

diff --git a/UnitTests/GitCommands.Tests/UserRepositoryHistory/RepositoryStorageTests.cs b/UnitTests/GitCommands.Tests/UserRepositoryHistory/RepositoryStorageTests.cs
index 0dc11..4bc47 100644
--- a/UnitTests/GitCommands.Tests/UserRepositoryHistory/RepositoryStorageTests.cs
+++ b/UnitTests/GitCommands.Tests/UserRepositoryHistory/RepositoryStorageTests.cs
@@ -59,6 +59,7 @@ public void Load_should_return_collection()
                 {
                     Anchor = Repository.RepositoryAnchor.AllRecent,
                 }
+
             ];
             _repositorySerialiser.Deserialize(Arg.Any<string>()).Returns(x => history);

Test methodology

Tests

Merge strategy

I agree that the maintainer squash merge this PR (if the commit message is clear).


✒️ I contribute this code under The Developer Certificate of Origin.

@ghost ghost assigned gerhardol Nov 19, 2023
@gerhardol gerhardol force-pushed the feature/ide000028-init-list branch from 642766c to 4f0ff71 Compare November 26, 2023 08:57
@gerhardol gerhardol marked this pull request as ready for review November 26, 2023 08:58
@RussKie
Copy link
Member

RussKie commented Nov 26, 2023

Merge at will

@RussKie RussKie merged commit 1cfb0e4 into gitextensions:master Nov 26, 2023
@ghost ghost added this to the vNext milestone Nov 26, 2023
@gerhardol gerhardol deleted the feature/ide000028-init-list branch November 26, 2023 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants