From 28cd8a827a772bc9ce9eab91033852fa99bc6d78 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Mon, 19 May 2025 14:12:29 +0200 Subject: [PATCH] C#: Add more test examples for cs/missing-readonly-modifier. --- .../MissedReadonlyOpportunity.cs | 55 +++++++++++++++++++ .../MissedReadonlyOpportunity.expected | 11 ++++ 2 files changed, 66 insertions(+) diff --git a/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.cs b/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.cs index bfe6b3243d4..a365feec5e3 100644 --- a/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.cs +++ b/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.cs @@ -2,22 +2,26 @@ class MissedReadonlyOpportunity { public int Bad1; // $ Alert public T Bad2; // $ Alert + public Immutable Bad3; // $ Alert public readonly int Good1; public readonly int Good2 = 0; public const int Good3 = 0; public int Good4; public readonly T Good5; public T Good6; + public Mutable Good7; public MissedReadonlyOpportunity(int i, T t) { Bad1 = i; Bad2 = t; + Bad3 = new Immutable(); Good1 = i; Good2 = i; Good4 = i; Good5 = t; Good6 = t; + Good7 = new Mutable(); } public void M(int i) @@ -27,3 +31,54 @@ class MissedReadonlyOpportunity x.Good6 = false; } } + +struct Mutable +{ + private int x; + public int Mutate() + { + x = x + 1; + return x; + } +} + +readonly struct Immutable { } + +class Tree +{ + private Tree? Parent; + private Tree? Left; // $ Alert + private readonly Tree? Right; + + public Tree(Tree left, Tree right) + { + this.Left = left; + this.Right = right; + left.Parent = this; + right.Parent = this; + } + + public Tree() + { + Left = null; + Right = null; + } +} + +class StaticFields +{ + static int X; // $ Alert + static int Y; + + // Static constructor + static StaticFields() + { + X = 0; + } + + // Instance constructor + public StaticFields(int y) + { + Y = y; + } +} diff --git a/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.expected b/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.expected index 680a571e775..620b6b2dd11 100644 --- a/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.expected +++ b/csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.expected @@ -1,3 +1,14 @@ +#select | MissedReadonlyOpportunity.cs:3:16:3:19 | Bad1 | Field 'Bad1' can be 'readonly'. | | MissedReadonlyOpportunity.cs:4:14:4:17 | Bad2 | Field 'Bad2' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:5:22:5:25 | Bad3 | Field 'Bad3' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:12:20:12:24 | Good7 | Field 'Good7' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:49:19:49:24 | Parent | Field 'Parent' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:50:19:50:22 | Left | Field 'Left' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:70:16:70:16 | X | Field 'X' can be 'readonly'. | +| MissedReadonlyOpportunity.cs:71:16:71:16 | Y | Field 'Y' can be 'readonly'. | | MissedReadonlyOpportunityBad.cs:3:9:3:13 | Field | Field 'Field' can be 'readonly'. | +testFailures +| MissedReadonlyOpportunity.cs:12:20:12:24 | Field 'Good7' can be 'readonly'. | Unexpected result: Alert | +| MissedReadonlyOpportunity.cs:49:19:49:24 | Field 'Parent' can be 'readonly'. | Unexpected result: Alert | +| MissedReadonlyOpportunity.cs:71:16:71:16 | Field 'Y' can be 'readonly'. | Unexpected result: Alert |