|
18 | 18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | 19 | */ |
20 | 20 |
|
21 | | -namespace SonarAnalyzer.Rules.CSharp |
| 21 | +namespace SonarAnalyzer.Rules.CSharp; |
| 22 | + |
| 23 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 24 | +public sealed class FieldsShouldBeEncapsulatedInProperties : SonarDiagnosticAnalyzer |
22 | 25 | { |
23 | | - [DiagnosticAnalyzer(LanguageNames.CSharp)] |
24 | | - public sealed class FieldsShouldBeEncapsulatedInProperties : SonarDiagnosticAnalyzer |
25 | | - { |
26 | | - private const string DiagnosticId = "S1104"; |
27 | | - private const string MessageFormat = "Make this field 'private' and encapsulate it in a 'public' property."; |
| 26 | + private const string DiagnosticId = "S1104"; |
| 27 | + private const string MessageFormat = "Make this field 'private' and encapsulate it in a 'public' property."; |
| 28 | + |
| 29 | + private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat); |
28 | 30 |
|
29 | | - private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat); |
| 31 | + private static readonly ISet<SyntaxKind> ValidModifiers = new HashSet<SyntaxKind> |
| 32 | + { |
| 33 | + SyntaxKind.PrivateKeyword, |
| 34 | + SyntaxKind.ProtectedKeyword, |
| 35 | + SyntaxKind.InternalKeyword, |
| 36 | + SyntaxKind.ReadOnlyKeyword, |
| 37 | + SyntaxKind.ConstKeyword |
| 38 | + }; |
30 | 39 |
|
31 | | - public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); |
| 40 | + private static readonly ImmutableArray<KnownType> IgnoredTypes = ImmutableArray.Create( |
| 41 | + KnownType.UnityEngine_MonoBehaviour, |
| 42 | + KnownType.UnityEngine_ScriptableObject); |
32 | 43 |
|
33 | | - private static readonly ISet<SyntaxKind> ValidModifiers = new HashSet<SyntaxKind> |
34 | | - { |
35 | | - SyntaxKind.PrivateKeyword, |
36 | | - SyntaxKind.ProtectedKeyword, |
37 | | - SyntaxKind.InternalKeyword, |
38 | | - SyntaxKind.ReadOnlyKeyword, |
39 | | - SyntaxKind.ConstKeyword |
40 | | - }; |
| 44 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); |
41 | 45 |
|
42 | | - protected override void Initialize(SonarAnalysisContext context) => |
43 | | - context.RegisterNodeAction( |
44 | | - c => |
| 46 | + protected override void Initialize(SonarAnalysisContext context) => |
| 47 | + context.RegisterNodeAction( |
| 48 | + c => |
| 49 | + { |
| 50 | + var fieldDeclaration = (FieldDeclarationSyntax)c.Node; |
| 51 | + if (fieldDeclaration.Modifiers.Any(m => ValidModifiers.Contains(m.Kind()))) |
45 | 52 | { |
46 | | - var fieldDeclaration = (FieldDeclarationSyntax)c.Node; |
47 | | - if (fieldDeclaration.Modifiers.Any(m => ValidModifiers.Contains(m.Kind()))) |
48 | | - { |
49 | | - return; |
50 | | - } |
| 53 | + return; |
| 54 | + } |
51 | 55 |
|
52 | | - var firstVariable = fieldDeclaration.Declaration.Variables[0]; |
53 | | - var symbol = c.SemanticModel.GetDeclaredSymbol(firstVariable); |
54 | | - var parentSymbol = c.SemanticModel.GetDeclaredSymbol(fieldDeclaration.Parent); |
55 | | - if (parentSymbol.HasAttribute(KnownType.System_Runtime_InteropServices_StructLayoutAttribute) || Serializable(symbol, parentSymbol)) |
56 | | - { |
57 | | - return; |
58 | | - } |
| 56 | + var firstVariable = fieldDeclaration.Declaration.Variables[0]; |
| 57 | + var symbol = c.SemanticModel.GetDeclaredSymbol(firstVariable); |
| 58 | + var parentSymbol = c.SemanticModel.GetDeclaredSymbol(fieldDeclaration.Parent); |
| 59 | + if (symbol.ContainingType.DerivesFromAny(IgnoredTypes) |
| 60 | + || parentSymbol.HasAttribute(KnownType.System_Runtime_InteropServices_StructLayoutAttribute) |
| 61 | + || Serializable(symbol, parentSymbol)) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
59 | 65 |
|
60 | | - if (symbol.GetEffectiveAccessibility() == Accessibility.Public) |
61 | | - { |
62 | | - c.ReportIssue(Rule, firstVariable); |
63 | | - } |
64 | | - }, |
65 | | - SyntaxKind.FieldDeclaration); |
| 66 | + if (symbol.GetEffectiveAccessibility() == Accessibility.Public) |
| 67 | + { |
| 68 | + c.ReportIssue(Rule, firstVariable); |
| 69 | + } |
| 70 | + }, |
| 71 | + SyntaxKind.FieldDeclaration); |
66 | 72 |
|
67 | | - private static bool Serializable(ISymbol symbol, ISymbol parentSymbol) => |
68 | | - parentSymbol.HasAttribute(KnownType.System_SerializableAttribute) |
69 | | - && !symbol.HasAttribute(KnownType.System_NonSerializedAttribute); |
70 | | - } |
| 73 | + private static bool Serializable(ISymbol symbol, ISymbol parentSymbol) => |
| 74 | + parentSymbol.HasAttribute(KnownType.System_SerializableAttribute) |
| 75 | + && !symbol.HasAttribute(KnownType.System_NonSerializedAttribute); |
71 | 76 | } |
0 commit comments