Disabling a specific compiler warning in VS Code

浪尽此生 提交于 2021-02-07 04:55:16

问题


I want to know how to suppress a specific compiler warning within VS Code for the entire project.
I have seen this queston: Is it possible to disable specific compiler warnings? but it is for Visual studio, not Visual Studio Code.

Here are the answers that where recommended in the question linked above:
1. Solution Explorer > View > Properties > Build > Suppress Warnings
and
2. #pragma warning disable warning-list

For #1: I can't find the Solution Explorer anywhere within VS Code.

For #2 This only works if I include it at the top of each of the scripts. I need a way to do so for the entire Project.

Updates:

I tried using <noWarn>01699,8019</noWarn> in my .csproj files, but no go.

After reviewing the last changes, I noticed that it had reverted to <noWarn>0169</noWarn>. I then realised that what I needed was <noWarn>0169;8019</noWarn>. Switching the , for a ; Solved the problem.

Well, it turns out that the above solution didn't work after all. As soon as I restarted VS Code, all the warnings came back. Maybe the error code I need isn't 8019, even though it worked as the error code within a #pragma statement. Are the codes used within a <noWarn> different than the codes used at the end of a #pragma statement?

For those saying too switch to VS Community, that's not the point. I'm using VS Code AS a text editor with Unity Editor. I'm looking for which file I need to change and what changes need to be made to apply a statement like #pragma warning disable 8019 to the entire project.


回答1:


I was able to get this to work. My solution looks something like this:

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.1</RuntimeFrameworkVersion>
    <NoWarn>0169;8019</NoWarn>
</PropertyGroup>

<NoWarn> is PascalCase rather than camelCase, and the element is nested inside of a <PropertyGroup>.




回答2:


Thanks to some code posted by user rakkarage on the Unity forums I was finally able to fix this by creating the following editor script in my Unity project:

using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
class FixCSProjs : AssetPostprocessor
{
    private static void OnGeneratedCSProjectFiles() {
        Debug.Log("Fixing csproj files...");
        var dir = Directory.GetCurrentDirectory();
        var files = Directory.GetFiles(dir, "*.csproj");

        foreach (var file in files) {
            FixProject(file);
        }
    }

    static bool FixProject(string file) {
        var text = File.ReadAllText(file);
        var find = "<NoWarn>([0-9;]+)<\\/NoWarn>";
        var replace = "<NoWarn>$1;3003;3009</NoWarn>";

        if (Regex.IsMatch(text, find)) {
            text = Regex.Replace(text, find, replace);
            File.WriteAllText(file, text);
            return true;

        } else {
            return false;
        }
    }
}

That will add extra warning codes to the NoWarn attribute of each csproj file every time they're automatically generated by Unity. This has the important advantage that it does not involve manually editing files automatically generated by Unity, meaning it's more robust and is appropriate to include in a code repository such as git.

In my case I'm disabling warnings 3003 and 3009 (the CLS-compliance warnings) but just replace 3003;3009 in my code with whatever semicolon-separated warning codes you want and it should do the trick.

I'd also like to say that this is a heinous solution and I really wish there was a better, cleaner way of accomplishing this.



来源:https://stackoverflow.com/questions/35002945/disabling-a-specific-compiler-warning-in-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!