1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+ // See the LICENSE file in the project root for more information.
4+
15using System ;
26using System . Collections . Generic ;
37using System . Collections . Immutable ;
@@ -46,8 +50,6 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context) {
4650 var codeContextSymbol = context . Compilation . GetTypeByMetadataName ( "IronPython.Runtime.CodeContext" ) ;
4751 var siteLocalStorageSymbol = context . Compilation . GetTypeByMetadataName ( "IronPython.Runtime.SiteLocalStorage" ) ;
4852 var notNullAttributeSymbol = context . Compilation . GetTypeByMetadataName ( "Microsoft.Scripting.Runtime.NotNullAttribute" ) ;
49- var disallowNullAttributeSymbol = context . Compilation . GetTypeByMetadataName ( "System.Diagnostics.CodeAnalysis.DisallowNullAttribute" ) ;
50- var allowNullAttributeSymbol = context . Compilation . GetTypeByMetadataName ( "System.Diagnostics.CodeAnalysis.AllowNullAttribute" ) ;
5153 var bytesLikeAttributeSymbol = context . Compilation . GetTypeByMetadataName ( "IronPython.Runtime.BytesLikeAttribute" ) ;
5254
5355 var byteType = context . Compilation . GetTypeByMetadataName ( "System.Byte" ) ;
@@ -59,8 +61,9 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context) {
5961
6062 foreach ( IParameterSymbol parameterSymbol in methodSymbol . Parameters ) {
6163 if ( parameterSymbol . GetAttributes ( ) . Any ( x => x . AttributeClass . Equals ( bytesLikeAttributeSymbol ) )
62- && ! parameterSymbol . Type . Equals ( ibufferProtocolType )
63- && ! parameterSymbol . Type . Equals ( ireadOnlyListOfByteType ) && ! parameterSymbol . Type . Equals ( ilistOfByteType ) ) {
64+ && ! parameterSymbol . Type . Equals ( ibufferProtocolType )
65+ && ! parameterSymbol . Type . Equals ( ireadOnlyListOfByteType )
66+ && ! parameterSymbol . Type . Equals ( ilistOfByteType ) ) {
6467 var diagnostic = Diagnostic . Create ( Rule3 , parameterSymbol . Locations [ 0 ] , parameterSymbol . Name , parameterSymbol . Type . MetadataName ) ;
6568 context . ReportDiagnostic ( diagnostic ) ;
6669 continue ;
@@ -70,18 +73,25 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context) {
7073 if ( SymbolEqualityComparer . Default . Equals ( parameterSymbol . Type . BaseType , siteLocalStorageSymbol ) ) continue ;
7174 if ( parameterSymbol . NullableAnnotation == NullableAnnotation . NotAnnotated ) {
7275 if ( ! parameterSymbol . GetAttributes ( ) . Any ( x => x . AttributeClass . Equals ( notNullAttributeSymbol ) )
73- && ! parameterSymbol . GetAttributes ( ) . Any ( x => x . AttributeClass . Equals ( allowNullAttributeSymbol ) ) ) {
76+ && ! parameterSymbol . GetAttributes ( ) . Any ( x => IsAllowNull ( x . AttributeClass ) ) ) {
7477 var diagnostic = Diagnostic . Create ( Rule1 , parameterSymbol . Locations [ 0 ] , parameterSymbol . Name ) ;
7578 context . ReportDiagnostic ( diagnostic ) ;
7679 }
7780 } else if ( parameterSymbol . NullableAnnotation == NullableAnnotation . Annotated ) {
7881 if ( parameterSymbol . GetAttributes ( ) . Any ( x => x . AttributeClass . Equals ( notNullAttributeSymbol ) )
79- && ! parameterSymbol . GetAttributes ( ) . Any ( x => x . AttributeClass . Equals ( disallowNullAttributeSymbol ) ) ) {
82+ && ! parameterSymbol . GetAttributes ( ) . Any ( x => IsDisallowNull ( x . AttributeClass ) ) ) {
8083 var diagnostic = Diagnostic . Create ( Rule2 , parameterSymbol . Locations [ 0 ] , parameterSymbol . Name ) ;
8184 context . ReportDiagnostic ( diagnostic ) ;
8285 }
8386 }
8487 }
88+
89+ bool IsAllowNull ( INamedTypeSymbol symbol ) {
90+ return symbol . ToString ( ) == "System.Diagnostics.CodeAnalysis.AllowNullAttribute" ;
91+ }
92+ bool IsDisallowNull ( INamedTypeSymbol symbol ) {
93+ return symbol . ToString ( ) == "System.Diagnostics.CodeAnalysis.DisallowNullAttribute" ;
94+ }
8595 }
8696
8797#pragma warning restore RS1024 // Compare symbols correctly
0 commit comments