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

CA1823 incorrectly flags const used as a fixed buffer length #37593

Open
stephentoub opened this issue Jul 30, 2019 · 6 comments
Open

CA1823 incorrectly flags const used as a fixed buffer length #37593

stephentoub opened this issue Jul 30, 2019 · 6 comments

Comments

@stephentoub
Copy link
Member

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.4

Diagnostic ID

CA1823

Repro steps

using System.Runtime.InteropServices;

class Program
{
    static void Main() { }

    private const int SomeLength = 16;

    [StructLayout(LayoutKind.Sequential)]
    internal unsafe struct SomeStruct
    {
        internal fixed byte FixedBuffer[SomeLength];
    }
}

Expected behavior

No warnings.

Actual behavior

The CA1823 analyzer flags the SomeLength const as being unused:

Program.cs(7,23,7,33): warning CA1823: Unused field 'SomeLength'.

even though obviously it is being used.

@mavasani
Copy link
Contributor

mavasani commented Jul 30, 2019

@jinujoseph @vatsalyaagrawal Can we port this issue to Roslyn repo? The issue does not seem specific to this analyzer. Seems like there is no IFieldReferenceOperation being generated for SomeLength usage in internal fixed byte FixedBuffer[SomeLength];.

Even Find All References also does not seem to find this reference to SomeLength, and the reference is also not colored as a normal reference would be, so it is very likely that the underlying compiler bound tree does not have a bound node representing this field reference.

@vatsalyaagrawal vatsalyaagrawal transferred this issue from dotnet/roslyn-analyzers Jul 30, 2019
@stephentoub
Copy link
Member Author

Even Find All References also does not seem to find this reference to SomeLength

Yeah, and the Rename support doesn't fix the reference, IDE0051 flags it as unused, Go to Definition on the reference fails, etc.

@CyrusNajmabadi
Copy link
Member

QuiclInfo doesn't work either. So this is likely a deeper SemanticModel issue, not just an IOp issue.

@jinujoseph jinujoseph added this to the 16.3 milestone Jul 30, 2019
@mavasani mavasani removed this from the 16.3 milestone Jul 30, 2019
@mavasani mavasani removed their assignment Jul 30, 2019
@mavasani
Copy link
Contributor

@CyrusNajmabadi is correct - this is a deeper issue in the compiler layer. Moved to Area-Compilers.

@gafter gafter added this to the Compiler.Next milestone Aug 1, 2019
@sharwell
Copy link
Member

sharwell commented Jul 6, 2020

The underlying semantic model issue is likely #29224

@gwr
Copy link

gwr commented Aug 18, 2024

FYI, there's an instance of this warning in PR dotnet/runtime#105403
src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFs.Definitions.cs
Would be nice to remove that pragma once this is fixed.
Let me know if a separate issue is wanted for that.

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

No branches or pull requests

8 participants