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

SA1010 false positive when list syntax is used in a field initializer #3687

Closed
AArnott opened this issue Aug 10, 2023 · 6 comments · Fixed by #3745
Closed

SA1010 false positive when list syntax is used in a field initializer #3687

AArnott opened this issue Aug 10, 2023 · 6 comments · Fixed by #3745

Comments

@AArnott
Copy link
Contributor

AArnott commented Aug 10, 2023

The following line produces a SA1010 warning claiming that the open square bracket should not be preceded by a space:

    protected static readonly string[] DefaultMetadataPaths = [MetadataPath, WdkMetadataPath];

This is using 1.2.0.507.

@sharwell sharwell added the c# 12 label Aug 11, 2023
@MiYanni
Copy link

MiYanni commented Sep 28, 2023

I hit this when telling VS to make fixes for Use collection initializers (IDE0028). It will automatically create statements that trigger SA1010. For example:

List<string> foo = [];

@dciborow
Copy link

dciborow commented Nov 4, 2023

Hit this in a few places.

ctors = [.. ctors.OrderByDescending(info => info.GetParameters().Length)];
object result = TypesManager.InvokeGenericStaticMethod(typeof(ServiceLocator), nameof(ResolveMock), [type]);
if (request.OtherEmails != null)
{
    emails = [.. request.OtherEmails.Split([';', ','])];
}
        public static readonly string[] AllBlastRadiusStrings = [SingleNode, SingleCustomer, SingleScaleUnitSlicePartition, SingleScaleUnit, SingleRack, SingleCluster, SingleAZ, SingleRegion, MultipleRacks, MultipleClusters, MultipleAZs, MultipleRegions, Global, Other];

@bjornhellander
Copy link
Contributor

I assume that .NET 8 and C# 12 will be released very soon now. This fix is probably trivial, but what do you think about adding a new test project, @sharwell? Should it wait until you are done with your updates to the test projects (#3675 (comment)), or can it be done now? If you would like to wait, do you have any estimate of when you will be done?

@DavidArno
Copy link

.NET 8 and C# 12 were released yesterday and so this is now an issue with production code. For now, I've had to disable the SA1010 rule for our code until this can be fixed. Please can this be fixed as quickly as possible.

@commonsensesoftware
Copy link

I'm unsure if all the various cases are being tracked or accounted for, but I thought I would report that SA1010 also fires during coalesce, coalesce assign, and ternary operations.

List<string>? list = default;

// fires on coalesce
List<string> otherList = list ?? [];

// fires on ternary
List<string> anotherList = list is null ? [] : list;

// fires on coalesce assigement
list ??= [];

bjornhellander added a commit to bjornhellander/StyleCopAnalyzers that referenced this issue Dec 6, 2023
@sharwell sharwell added the fixed label Dec 15, 2023
@sharwell sharwell added this to the 1.2-beta.next milestone Dec 15, 2023
@YoshiRulz
Copy link

see also #3503

koal44 added a commit to koal44/wpfui that referenced this issue Apr 6, 2024
There's still a lot of IDE0028 warnings as the auto fix really wants to use the C#12 collection expressions, which btw will trigger SA1010 warnings (DotNetAnalyzers/StyleCopAnalyzers#3687)
pomianowski pushed a commit to lepoco/wpfui that referenced this issue Apr 14, 2024
* Fix null analyzer complaints: CS8602, CS8618, CS8603, CS8622, IDE0031

* Hide warnings related to IDE0008 related to using `var` keyword

* Removed Coersion logic from IconSourceElementConverter, and placed in IconElement and IcounSourceElement classes instead

* Cleaning single-line warnings: SA1005

- Removed commented-out Toolbox attributes
- Replaced single-line commented-out code with multi-line

* Fix missing braces or single-lined braces

- Add stylecop supression of 1502 "element should not be on a single line"
- Wrap with braces where needed

* Fix warnings on registered dependency properties

WPF0100, WPF0005, WPF0012

* Fix documentation warnings

WPF0108, WPF0060, SA1642, SA1623

* Fix warnings about using language keywords

- Use string, double, int rather than String, Double, Int32, etc
- remove 'this' keyword when context is obvious

* Fix warnings where attributes are on a single line SA1133

* When setting DPs, favor SetCurrentValue over the CLR setter. WPF0041

* Remove unnecessary things (parenthesis, assignments, etc). SA1119, IDE0059

* Fix order of operation warnings

* Do not use regions SA1124. Clean VirtualizingPanelBase of warnings

* Turn protected control fields into protected properties. fix some documentation warnings

* Cleanup warnings in Interop classes and suppress warnings related to uper-casing and private fields

* Continue cleaning up warnings in Wpf.Ui

* Edit .editorconfig to suppress warnings related to valueconverters

* Cleanup warning related to 'var'

* Fix warnings

* Split ContextMenu style from its loader dictionary

There were some issue when having a class called ContextMenu that wasn't actually a contextmenu but  a ResourceDictionary

* Resolve "expression value is never used" IDE0058 warnings

* Fix warnings related to collection initialization.

There's still a lot of IDE0028 warnings as the auto fix really wants to use the C#12 collection expressions, which btw will trigger SA1010 warnings (DotNetAnalyzers/StyleCopAnalyzers#3687)

* Remove unused suppressions

* Supress IDE0290 (Use primary constructor). Just didn't seem necessary.

* IDE0009 should be configured through dotnet_styles

* Separate out the loader component from the Menu.xaml style.

Justification: Menu was not a control.Menu, it's more like a hack to change private api settings.

* Fix warnings on NavigationView.NavigationParent attached property

* Fix warnings related to setting refernce types as default values. for dependency properties. Also fixed some enum warnings

* Fix warnings

* Fix regression error where the ContentDialog wouldn't appear and change name of ContentPresenter to DialogHost

* Remove obsolete forceBacground parameters. Breaking changes

* Remove redundant #if DEBUG directives around Debug.WriteLine calls

* Fix some analyzer warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants