Rider如何针对一个项目禁用某一类错误检查(Inspections)的办法

      Rider的Inspections确实是非常好的功能,但是也并非每一种检查都适合当前项目,有的检查对于当前项目可能是没有意义的,比如对于一个基础库,将很多未使用的属性、方法、类等设置为public是很合理的,但是Rider会高亮提醒,这可能不是我们想要的,所以我们可能想针对当前项目禁止这种检查。

      最简单的办法可能就是将光标移动到波浪线上,然后点击左侧的“点亮”图标(或者你的快捷键)选择修复或者使用注释禁用。但是这需要每个位置都作一次,更好的办法是使用.editorconfig配置。比如下面这个配置文件:

[*.cs]
resharper_unused_type_global_highlighting = none
resharper_unused_member_in_super_global_highlighting = none
resharper_gc_suppress_finalize_for_type_without_destructor_highlighting = none
resharper_member_can_be_private_global_highlighting = none
resharper_invert_if_highlighting = none
resharper_convert_switch_statement_to_switch_expression_highlighting = none
resharper_switch_statement_missing_some_enum_cases_no_default_highlighting = none
resharper_unused_member_global_highlighting = none
resharper_unused_field_compiler_highlighting = none
resharper_switch_statement_handles_some_known_enum_values_with_default_highlighting = none

      这里将.editorconfig所在目录以及其子目录下面的所有cs代码文件的Inspections禁用掉了一部分。本身Rider有上千种检查,要准确的找到这个检查叫什么名字,靠猜是很费劲的,所以这里提供一个百试不爽的办法,仅供参考。首先你要收藏Rider的这个官方文档:

      C# : https://www.jetbrains.com/help/rider/Reference__Code_Inspections_CSHARP.html

      C++ :https://www.jetbrains.com/help/rider/Reference__Code_Inspections_CPP.html

      其他语言以此类推。比如下面这一条。

Some values of the enum are not processed inside 'switch' statement and are handled via default section

SwitchStatementHandlesSomeKnownEnumValuesWithDefault

resharper_switch_statement_handles_some_known_enum_values_with_default_highlightin

 

     如图所示有这样一个提醒。此时可能我们并不想处理这个提醒。所以我们可以点击左侧“点亮”图标或者你的快捷键选择使用注释来禁用这一次:

     此时你可以看到,Rider自动帮你添加了一个注释:

    /// <summary>
    /// Gets the type of the underlying.
    /// </summary>
    /// <param name="memberInfo">The member information.</param>
    /// <returns>Type.</returns>
    /// <exception cref="System.ArgumentException">Input MemberInfo must be if type EventInfo, FieldInfo, MethodInfo, or PropertyInfo</exception>
    public static Type GetUnderlyingType(this MemberInfo memberInfo)
    {
        // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
        switch (memberInfo.MemberType)
        {
            case MemberTypes.Event:
                return ((EventInfo)memberInfo).EventHandlerType!;
            case MemberTypes.Field:
                return ((FieldInfo)memberInfo).FieldType;
            case MemberTypes.Method:
                return ((MethodInfo)memberInfo).ReturnType;
            case MemberTypes.Property:
                return ((PropertyInfo)memberInfo).PropertyType;
            default:
                throw new ArgumentException
                (
                    "Input MemberInfo must be if type EventInfo, FieldInfo, MethodInfo, or PropertyInfo"
                );
        }
    }

      注释中的SwitchStatementHandlesSomeKnownEnumValuesWithDefault就是我们要关注的关键字。此时只需要拿着这个关键字去官方文档搜索一下即可:

      下方的字符串即可用于在.editorconfig中用于配置禁用某个Inspection。我也尝试过直接配置SwitchStatementHandlesSomeKnownEnumValuesWithDefault到.editorconfig中,但是似乎并没有效果。

      

posted @ 2024-07-02 09:56  bodong  阅读(6)  评论(0编辑  收藏  举报