Skip to content

Commit 6240b3b

Browse files
Copilotgewarren
andcommitted
Add C# code snippets from kurnakovv's PRs to all-rules project
Co-authored-by: gewarren <[email protected]>
1 parent a68c5d6 commit 6240b3b

File tree

17 files changed

+684
-0
lines changed

17 files changed

+684
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace ca1720
4+
{
5+
//<snippet1>
6+
// This code violates the rule.
7+
public class Short
8+
{
9+
public int Int32 { get; set; }
10+
public Guid Guid { get; set; }
11+
12+
public void Float(int int32) { }
13+
}
14+
//</snippet1>
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace ca1725
4+
{
5+
//<snippet1>
6+
public interface IUserService
7+
{
8+
int GetAge(int id);
9+
}
10+
11+
public class UserService : IUserService
12+
{
13+
// Violates CA1725: Parameter name should match the base declaration ('id')
14+
// for consistency with IUserService
15+
public int GetAge(int userId)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
}
20+
//</snippet1>
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace ca1727
4+
{
5+
//<snippet1>
6+
public class UserService
7+
{
8+
private readonly ILogger<UserService> _logger;
9+
10+
public UserService(ILogger<UserService> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public void Create(string firstName, string lastName)
16+
{
17+
// This code violates the rule.
18+
_logger.LogInformation("Creating user {firstName} {lastName}", firstName, lastName);
19+
20+
// This code satisfies the rule.
21+
_logger.LogInformation("Creating user {FirstName} {LastName}", firstName, lastName);
22+
}
23+
}
24+
//</snippet1>
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ca1822
5+
{
6+
//<snippet1>
7+
public class Printer
8+
{
9+
private readonly List<char> _items = [
10+
'H', 'e', 'l', 'l', 'o',
11+
];
12+
13+
public void PrintHello()
14+
{
15+
BadPrintHelloInternal();
16+
GoodPrintHelloInternal();
17+
GoodPrintHelloStaticInternal();
18+
}
19+
20+
// This method violates the rule.
21+
private void BadPrintHelloInternal()
22+
{
23+
Console.WriteLine("Hello");
24+
}
25+
26+
// This methods satisfies the rule.
27+
private void GoodPrintHelloInternal()
28+
{
29+
Console.WriteLine(string.Join(string.Empty, this._items));
30+
}
31+
32+
private static void GoodPrintHelloStaticInternal()
33+
{
34+
Console.WriteLine("Hello");
35+
}
36+
}
37+
//</snippet1>
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace ca1823
2+
{
3+
//<snippet1>
4+
public class User
5+
{
6+
private readonly string _firstName;
7+
private readonly string _lastName;
8+
9+
// CA1823: Unused field '_age'
10+
private readonly int _age;
11+
12+
public User(string firstName, string lastName)
13+
{
14+
_firstName = firstName;
15+
_lastName = lastName;
16+
}
17+
18+
public string GetFullName()
19+
{
20+
return $"My name is {_firstName} {_lastName}";
21+
}
22+
}
23+
//</snippet1>
24+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace ca1844
7+
{
8+
//<snippet1>
9+
// This class violates the rule.
10+
public class BadStream : Stream
11+
{
12+
private readonly Stream _innerStream;
13+
14+
public BadStream(Stream innerStream)
15+
{
16+
_innerStream = innerStream;
17+
}
18+
19+
public override bool CanRead => _innerStream.CanRead;
20+
public override bool CanSeek => _innerStream.CanSeek;
21+
public override bool CanWrite => _innerStream.CanWrite;
22+
public override long Length => _innerStream.Length;
23+
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }
24+
25+
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
26+
{
27+
// ...
28+
return await _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
29+
}
30+
31+
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
32+
{
33+
// ...
34+
await _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
35+
}
36+
37+
// Other required overrides
38+
public override void Flush() => _innerStream.Flush();
39+
public override int Read(byte[] buffer, int offset, int count) => _innerStream.Read(buffer, offset, count);
40+
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
41+
public override void SetLength(long value) => _innerStream.SetLength(value);
42+
public override void Write(byte[] buffer, int offset, int count) => _innerStream.Write(buffer, offset, count);
43+
}
44+
45+
// This class satisfies the rule.
46+
public class GoodStream : Stream
47+
{
48+
private readonly Stream _innerStream;
49+
50+
public GoodStream(Stream innerStream)
51+
{
52+
_innerStream = innerStream;
53+
}
54+
55+
public override bool CanRead => _innerStream.CanRead;
56+
public override bool CanSeek => _innerStream.CanSeek;
57+
public override bool CanWrite => _innerStream.CanWrite;
58+
public override long Length => _innerStream.Length;
59+
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }
60+
61+
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
62+
{
63+
// ...
64+
return await _innerStream.ReadAsync(buffer, cancellationToken);
65+
}
66+
67+
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
68+
{
69+
// ...
70+
await _innerStream.WriteAsync(buffer, cancellationToken);
71+
}
72+
73+
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
74+
{
75+
return await this.ReadAsync(buffer.AsMemory(offset, count), cancellationToken);
76+
}
77+
78+
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
79+
{
80+
await this.WriteAsync(buffer.AsMemory(offset, count), cancellationToken);
81+
}
82+
83+
// Other required overrides
84+
public override void Flush() => _innerStream.Flush();
85+
public override int Read(byte[] buffer, int offset, int count) => _innerStream.Read(buffer, offset, count);
86+
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
87+
public override void SetLength(long value) => _innerStream.SetLength(value);
88+
public override void Write(byte[] buffer, int offset, int count) => _innerStream.Write(buffer, offset, count);
89+
}
90+
//</snippet1>
91+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace ca2008
5+
{
6+
//<snippet1>
7+
public class Example
8+
{
9+
public void ProcessData()
10+
{
11+
// This code violates the rule.
12+
var badTask = Task.Factory.StartNew(
13+
() =>
14+
{
15+
// ...
16+
}
17+
);
18+
badTask.ContinueWith(
19+
t =>
20+
{
21+
// ...
22+
}
23+
);
24+
25+
// This code satisfies the rule.
26+
var goodTask = Task.Factory.StartNew(
27+
() =>
28+
{
29+
// ...
30+
},
31+
CancellationToken.None,
32+
TaskCreationOptions.None,
33+
TaskScheduler.Default
34+
);
35+
goodTask.ContinueWith(
36+
t =>
37+
{
38+
// ...
39+
},
40+
CancellationToken.None,
41+
TaskContinuationOptions.None,
42+
TaskScheduler.Default
43+
);
44+
}
45+
}
46+
//</snippet1>
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Threading.Tasks;
2+
3+
namespace ca2012
4+
{
5+
//<snippet1>
6+
public class NumberValueTask
7+
{
8+
public async ValueTask<int> GetNumberAsync()
9+
{
10+
await Task.Delay(100);
11+
return 123;
12+
}
13+
14+
public async Task UseValueTaskIncorrectlyAsync()
15+
{
16+
// This code violates the rule,
17+
// because ValueTask is awaited multiple times
18+
ValueTask<int> numberValueTask = GetNumberAsync();
19+
20+
int first = await numberValueTask;
21+
int second = await numberValueTask; // <- illegal reuse
22+
23+
// ...
24+
}
25+
26+
// This code satisfies the rule.
27+
public async Task UseValueTaskCorrectlyAsync()
28+
{
29+
int first = await GetNumberAsync();
30+
int second = await GetNumberAsync();
31+
32+
// ..
33+
}
34+
35+
public async Task UseValueTaskAsTaskAsync()
36+
{
37+
ValueTask<int> numberValueTask = GetNumberAsync();
38+
39+
Task<int> numberTask = numberValueTask.AsTask();
40+
41+
int first = await numberTask;
42+
int second = await numberTask;
43+
44+
// ...
45+
}
46+
}
47+
//</snippet1>
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace ca2014
4+
{
5+
public class Example
6+
{
7+
//<snippet1>
8+
// This method violates the rule.
9+
public void ProcessDataBad()
10+
{
11+
for (int i = 0; i < 100; i++)
12+
{
13+
// CA2014: Potential stack overflow.
14+
// Move the stackalloc out of the loop.
15+
Span<int> buffer = stackalloc int[100];
16+
buffer[0] = i;
17+
18+
// ...
19+
}
20+
}
21+
22+
// This method satisfies the rule.
23+
public void ProcessDataGood()
24+
{
25+
Span<int> buffer = stackalloc int[100];
26+
27+
for (int i = 0; i < 100; i++)
28+
{
29+
buffer[0] = i;
30+
31+
// ...
32+
}
33+
}
34+
//</snippet1>
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace ca2201
4+
{
5+
//<snippet1>
6+
public class ExampleClass
7+
{
8+
public void BadMethod()
9+
{
10+
// This code violates the rule.
11+
throw new Exception();
12+
throw new NullReferenceException();
13+
throw new IndexOutOfRangeException();
14+
// ...
15+
}
16+
17+
public void GoodMethod()
18+
{
19+
// This code satisfies the rule.
20+
throw new ArgumentException();
21+
throw new ArgumentNullException();
22+
throw new InvalidOperationException();
23+
// ...
24+
}
25+
26+
// A minimal implementation of inheritance from Exception
27+
public class CustomException : Exception { }
28+
29+
public void AnotherGoodMethod()
30+
{
31+
// Or create your own type that derives from Exception
32+
// This code satisfies the rule too.
33+
throw new CustomException();
34+
}
35+
}
36+
//</snippet1>
37+
}

0 commit comments

Comments
 (0)