Programming

C #에서 "사용되지 않음"및 "할당되지 않음"경고 표시 안 함

procodes 2020. 8. 11. 21:37
반응형

C #에서 "사용되지 않음"및 "할당되지 않음"경고 표시 안 함


기본적으로 관리 코드에서 사용하는 이전 Windows ISAPI를 설명하는 C # 프로젝트에 HTTPSystemDefinitions.cs 파일이 있습니다.

여기에는 전부가 아니거나 코드에서 사용되는 ISAPI와 관련된 전체 구조 집합이 포함됩니다. 컴파일시 이러한 구조의 모든 필드 멤버는 다음과 같은 경고를 발생시킵니다.

경고 필드 'UnionSquare.ISAPI.HTTP_FILTER_PREPROC_HEADERS.SetHeader'는 할당되지 않으며 항상 기본값이 null입니다.

또는

경고 'UnionSquare.ISAPI.HTTP_FILTER_PREPROC_HEADERS.HttpStatus'필드는 사용되지 않습니다.

으로 비활성화 할 수 있습니까 #pragma warning disable? 그렇다면 해당 오류 번호는 무엇입니까? 내가 할 수있는 다른 것이 없다면? 이 파일에 대해이 작업을 수행하는 작업 만 다른 파일에서 오는 것과 같은 경고를받는 것이 중요합니다.

편집하다

예제 구조체 :-

struct HTTP_FILTER_PREPROC_HEADERS
{
    //
    //  For SF_NOTIFY_PREPROC_HEADERS, retrieves the specified header value.
    //  Header names should include the trailing ':'.  The special values
    //  'method', 'url' and 'version' can be used to retrieve the individual
    //  portions of the request line
    //

    internal GetHeaderDelegate GetHeader;
    internal SetHeaderDelegate SetHeader;
    internal AddHeaderDelegate AddHeader;

    UInt32  HttpStatus;               // New in 4.0, status for SEND_RESPONSE
    UInt32  dwReserved;               // New in 4.0
}

네, 억제 할 수 있습니다.

일반적으로 나는 경고를 억제하는 것을 반대하지만,이 경우 interop에 사용되는 구조체는 절대로 사용할 의도 (또는 사용할 수 없음)에도 불구하고 일부 필드가 있어야하기 때문에이 경우 정당화되어야한다고 생각합니다. .

일반적으로 이러한 두 가지 경고를 표시하지 않으려면 문제가되는 코드를 수정합니다. 첫 번째 ( "... 사용되지 않음")는 일반적으로 이전 버전의 코드에서 남은 코드 냄새입니다. 코드가 삭제되었지만 필드가 남아있을 수 있습니다.

두 번째는 일반적으로 잘못 사용 된 필드에 대한 코드 냄새입니다. 예를 들어 속성의 새 값을 다시 속성 자체에 잘못 쓰고 백업 필드에는 쓰지 않을 수 있습니다.


" Field XYZ is never used "에 대한 경고를 표시하지 않으려면 다음을 수행하십시오.

#pragma warning disable 0169
... field declaration
#pragma warning restore 0169

" 필드 XYZ가 할당되지 않고 항상 기본값 XX "에 대한 경고를 표시하지 않으려면 다음을 수행하십시오.

#pragma warning disable 0649
... field declaration
#pragma warning restore 0649

이러한 경고 번호를 직접 찾으려면 (즉, 0169 및 0649를 사용하는 방법을 어떻게 알았습니까?) 다음과 같이하십시오.

  • 코드를 정상적으로 컴파일하면 Visual Studio의 오류 목록에 몇 가지 경고가 추가됩니다.
  • 출력 창과 빌드 출력으로 전환하고 동일한 경고를 찾습니다.
  • 다음과 같은 관련 메시지에서 4 자리 경고 코드를 복사합니다.

    C : \ Dev \ VS.NET \ ConsoleApplication19 \ ConsoleApplication19 \ Program.cs ( 10,28 ) : warning CS 0649 : 'ConsoleApplication19.Program.dwReserved'필드는 할당되지 않으며 항상 기본값 0을 갖습니다.


주의 사항 : @Jon Hanna 의 의견에 따라이 질문과 답변을 찾는 사람을 위해 몇 가지 경고가있을 것입니다.

  • 우선, 경고를 억제하는 행위는 두통 때문에 약을 삼키는 것과 유사합니다. 물론, 때때로하는 것이 옳은 일일 수도 있지만, 포괄적 인 해결책은 아닙니다. 때때로 두통은 경고와 마찬가지로 가리지 않아야하는 실제 증상입니다. 빌드 출력에서 ​​경고를 맹목적으로 제거하는 대신 원인을 수정하여 경고를 처리하는 것이 항상 가장 좋습니다.
  • 경고를 표시하지 않으려면 위에서 설명한 패턴을 따르십시오. 첫 번째 코드 줄인은 해당 파일의 나머지 부분에 대한#pragma warning disable XYZK 경고 비활성화 하거나 적어도 해당 파일을#pragma warning restore XYZK 찾을 때까지 경고 비활성화합니다 . 이러한 경고를 비활성화하는 줄 수를 최소화하십시오. 위의 패턴은 한 줄에 대한 경고를 비활성화합니다.
  • 또한 Jon이 언급했듯이이 작업을 수행하는 이유에 대한 설명은 좋은 생각입니다. 경고를 비활성화하는 것은 원인없이 수행 할 때 분명히 코드 냄새이며, 주석은 향후 관리자가 왜 그렇게했는지 궁금해하거나 제거하고 경고를 수정하는 데 시간을 소비하는 것을 방지합니다.

Another "solution" to fix these warnings is by making the struct public. The warnings are not issued then because the compiler can't know whether or not the fields are being used (assigned) outside of the assembly.

That said, "interop" components should usually not be public, but rather internal or private.


I got VS to generate the implementation skeleton for System.ComponentModel.INotifyPropertyChanged and the events were implemented as fields which triggered the CS0067 warnings.

As an alternative to the solution given in the accepted answer I converted the fields into properties and the warning disappeared.

This makes sense since the property declarations syntax sugar are compiled into a field plus getter and/or setter methods (add/remove in my case) which reference the field. This satisfies the compiler and the warnings are not raised:

struct HTTP_FILTER_PREPROC_HEADERS
{
    //
    //  For SF_NOTIFY_PREPROC_HEADERS, retrieves the specified header value.
    //  Header names should include the trailing ':'.  The special values
    //  'method', 'url' and 'version' can be used to retrieve the individual
    //  portions of the request line
    //

    internal GetHeaderDelegate GetHeader {get;set;}
    internal SetHeaderDelegate SetHeader { get; set; }
    internal AddHeaderDelegate AddHeader { get; set; }

    UInt32 HttpStatus { get; set; }               // New in 4.0, status for SEND_RESPONSE
    UInt32 dwReserved { get; set; }               // New in 4.0
}

C/C++ users have (void)var; to suppress unused variables warnings. I just discovered you can also suppress unused variables warnings in C# with bitwise operators:

        uint test1 = 12345;
        test1 |= 0; // test1 is still 12345

        bool test2 = true;
        test2 &= false; // test2 is now false

Both expressions don't produce unused variable warnings in VS2010 C# 4.0 and Mono 2.10 compilers.

참고URL : https://stackoverflow.com/questions/3820985/suppressing-is-never-used-and-is-never-assigned-to-warnings-in-c-sharp

반응형