dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

WINDOWS Preprocessor not hiding CA1416 with target framework net8.0-windows #7329

Closed Maxyeah closed 1 month ago

Maxyeah commented 3 months ago

Version Used: VS2022 Version 17.8.4

Steps to Reproduce:

  1. Create Console Project with net8
  2. Add net8.0-windows to Target Frameworks (net8.0; net8.0-windows)
  3. Disable Autogenerate AssemblyInfo
  4. Add a class with SupportedOSPlatform Attribute for windows([System.Runtime.Versioning.SupportedOSPlatform("windows")])
  5. Instantiate class inside WINDOWS Preprocessor.
#if WINDOWS
    WindowsOnlyClass windowsOnlyClass = new WindowsOnlyClass();
#endif

The question is, why is it necessary to set the supported OS platform in the assembly info if the WINDOWS Preprocessor already hides the class instantiation for non windows framework targets.

Expected Behavior: Compiler will hide Warning due to WINDOWS Preprocessor.

Actual Behavior: Compiler warning CA1416: This call site is reachable on all platforms. 'WindowsOnlyClass' is only supported on: 'windows'.

Example Project: WindowsPreprocessor.zip

buyaa-n commented 1 month ago

This a known issue and it is currently not fixable as there is no way to get the #if-def conditional in analyzer until https://github.com/dotnet/roslyn/issues/24569 is fixed, see case 3 from https://github.com/dotnet/roslyn-analyzers/issues/5938

Closing as duplicate of https://github.com/dotnet/roslyn-analyzers/issues/5938