Skip to content

Commit 5a83331

Browse files
committed
Enable recommended .NET analysers.
1 parent 4b05541 commit 5a83331

23 files changed

+91
-28
lines changed

src/Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
<RepositoryUrl>https://github.com/mysql-net/MySqlConnector.git</RepositoryUrl>
1515
<DebugType>embedded</DebugType>
1616
<LangVersion>preview</LangVersion>
17+
<AnalysisLevel>latest-recommended</AnalysisLevel>
1718
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1819
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1920
<GenerateDocumentationFile>true</GenerateDocumentationFile>
20-
<NoWarn>$(NoWarn);1591;NU5105</NoWarn>
21+
<NoWarn>$(NoWarn);1591;CA1708;CA1835;CA2215;CA5397;NU5105</NoWarn>
2122
<MinVerDefaultPreReleasePhase>beta</MinVerDefaultPreReleasePhase>
2223
</PropertyGroup>
2324

src/MySqlConnector/Core/DbTypeMapping.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Globalization;
2+
13
namespace MySqlConnector.Core;
24

35
internal sealed class DbTypeMapping
@@ -16,7 +18,7 @@ public object DoConversion(object obj)
1618
{
1719
if (obj.GetType() == ClrType)
1820
return obj;
19-
return m_convert is null ? Convert.ChangeType(obj, ClrType)! : m_convert(obj);
21+
return m_convert is null ? Convert.ChangeType(obj, ClrType, CultureInfo.InvariantCulture)! : m_convert(obj);
2022
}
2123

2224
readonly Func<object, object>? m_convert;

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task ReadResultSetHeaderAsync(IOBehavior ioBehavior)
8585
try
8686
{
8787
int byteCount;
88-
while ((byteCount = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
88+
while ((byteCount = await stream.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None).ConfigureAwait(false)) > 0)
8989
{
9090
payload = new(new ArraySegment<byte>(buffer, 0, byteCount));
9191
await Session.SendReplyAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
@@ -286,6 +286,8 @@ public async Task<bool> ReadAsync(IOBehavior ioBehavior, CancellationToken cance
286286
public int Depth => 0;
287287
#pragma warning restore CA1822 // Mark members as static
288288

289+
#pragma warning disable CA2201 // Do not raise reserved exception types (IndexOutOfRangeException)
290+
289291
public string GetName(int ordinal)
290292
{
291293
if (ColumnDefinitions is null)

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace MySqlConnector.Core;
2222

23+
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
24+
2325
internal sealed class ServerSession
2426
{
2527
public ServerSession()
@@ -1029,7 +1031,11 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
10291031
try
10301032
{
10311033
ipAddresses = ioBehavior == IOBehavior.Asynchronous
1034+
#if NET6_0_OR_GREATER
1035+
? await Dns.GetHostAddressesAsync(hostName, cancellationToken).ConfigureAwait(false)
1036+
#else
10321037
? await Dns.GetHostAddressesAsync(hostName).ConfigureAwait(false)
1038+
#endif
10331039
: Dns.GetHostAddresses(hostName);
10341040
}
10351041
catch (SocketException)

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Text;
23
using MySqlConnector.Protocol;
34
using MySqlConnector.Protocol.Payloads;
@@ -19,18 +20,18 @@ private TypeMapper()
1920
m_mySqlDbTypeToColumnTypeMetadata = new();
2021

2122
// boolean
22-
var typeBoolean = AddDbTypeMapping(new(typeof(bool), new[] { DbType.Boolean }, convert: static o => Convert.ToBoolean(o)));
23+
var typeBoolean = AddDbTypeMapping(new(typeof(bool), new[] { DbType.Boolean }, convert: static o => Convert.ToBoolean(o, CultureInfo.InvariantCulture)));
2324
AddColumnTypeMetadata(new("TINYINT", typeBoolean, MySqlDbType.Bool, isUnsigned: false, length: 1, columnSize: 1, simpleDataTypeName: "BOOL", createFormat: "BOOL"));
2425

2526
// integers
26-
var typeSbyte = AddDbTypeMapping(new(typeof(sbyte), new[] { DbType.SByte }, convert: static o => Convert.ToSByte(o)));
27-
var typeByte = AddDbTypeMapping(new(typeof(byte), new[] { DbType.Byte }, convert: static o => Convert.ToByte(o)));
28-
var typeShort = AddDbTypeMapping(new(typeof(short), new[] { DbType.Int16 }, convert: static o => Convert.ToInt16(o)));
29-
var typeUshort = AddDbTypeMapping(new(typeof(ushort), new[] { DbType.UInt16 }, convert: static o => Convert.ToUInt16(o)));
30-
var typeInt = AddDbTypeMapping(new(typeof(int), new[] { DbType.Int32 }, convert: static o => Convert.ToInt32(o)));
31-
var typeUint = AddDbTypeMapping(new(typeof(uint), new[] { DbType.UInt32 }, convert: static o => Convert.ToUInt32(o)));
32-
var typeLong = AddDbTypeMapping(new(typeof(long), new[] { DbType.Int64 }, convert: static o => Convert.ToInt64(o)));
33-
var typeUlong = AddDbTypeMapping(new(typeof(ulong), new[] { DbType.UInt64 }, convert: static o => Convert.ToUInt64(o)));
27+
var typeSbyte = AddDbTypeMapping(new(typeof(sbyte), new[] { DbType.SByte }, convert: static o => Convert.ToSByte(o, CultureInfo.InvariantCulture)));
28+
var typeByte = AddDbTypeMapping(new(typeof(byte), new[] { DbType.Byte }, convert: static o => Convert.ToByte(o, CultureInfo.InvariantCulture)));
29+
var typeShort = AddDbTypeMapping(new(typeof(short), new[] { DbType.Int16 }, convert: static o => Convert.ToInt16(o, CultureInfo.InvariantCulture)));
30+
var typeUshort = AddDbTypeMapping(new(typeof(ushort), new[] { DbType.UInt16 }, convert: static o => Convert.ToUInt16(o, CultureInfo.InvariantCulture)));
31+
var typeInt = AddDbTypeMapping(new(typeof(int), new[] { DbType.Int32 }, convert: static o => Convert.ToInt32(o, CultureInfo.InvariantCulture)));
32+
var typeUint = AddDbTypeMapping(new(typeof(uint), new[] { DbType.UInt32 }, convert: static o => Convert.ToUInt32(o, CultureInfo.InvariantCulture)));
33+
var typeLong = AddDbTypeMapping(new(typeof(long), new[] { DbType.Int64 }, convert: static o => Convert.ToInt64(o, CultureInfo.InvariantCulture)));
34+
var typeUlong = AddDbTypeMapping(new(typeof(ulong), new[] { DbType.UInt64 }, convert: static o => Convert.ToUInt64(o, CultureInfo.InvariantCulture)));
3435
AddColumnTypeMetadata(new("TINYINT", typeSbyte, MySqlDbType.Byte, isUnsigned: false));
3536
AddColumnTypeMetadata(new("TINYINT", typeByte, MySqlDbType.UByte, isUnsigned: true, length: 1));
3637
AddColumnTypeMetadata(new("TINYINT", typeByte, MySqlDbType.UByte, isUnsigned: true));
@@ -45,9 +46,9 @@ private TypeMapper()
4546
AddColumnTypeMetadata(new("BIT", typeUlong, MySqlDbType.Bit));
4647

4748
// decimals
48-
var typeDecimal = AddDbTypeMapping(new(typeof(decimal), new[] { DbType.Decimal, DbType.Currency, DbType.VarNumeric }, convert: static o => Convert.ToDecimal(o)));
49-
var typeDouble = AddDbTypeMapping(new(typeof(double), new[] { DbType.Double }, convert: static o => Convert.ToDouble(o)));
50-
var typeFloat = AddDbTypeMapping(new(typeof(float), new[] { DbType.Single }, convert: static o => Convert.ToSingle(o)));
49+
var typeDecimal = AddDbTypeMapping(new(typeof(decimal), new[] { DbType.Decimal, DbType.Currency, DbType.VarNumeric }, convert: static o => Convert.ToDecimal(o, CultureInfo.InvariantCulture)));
50+
var typeDouble = AddDbTypeMapping(new(typeof(double), new[] { DbType.Double }, convert: static o => Convert.ToDouble(o, CultureInfo.InvariantCulture)));
51+
var typeFloat = AddDbTypeMapping(new(typeof(float), new[] { DbType.Single }, convert: static o => Convert.ToSingle(o, CultureInfo.InvariantCulture)));
5152
AddColumnTypeMetadata(new("DECIMAL", typeDecimal, MySqlDbType.NewDecimal, createFormat: "DECIMAL({0},{1});precision,scale"));
5253
AddColumnTypeMetadata(new("DECIMAL", typeDecimal, MySqlDbType.Decimal));
5354
AddColumnTypeMetadata(new("DOUBLE", typeDouble, MySqlDbType.Double));
@@ -97,7 +98,7 @@ private TypeMapper()
9798
#if NET6_0_OR_GREATER
9899
AddDbTypeMapping(new(typeof(TimeOnly), new[] { DbType.Time }));
99100
#endif
100-
var typeTime = AddDbTypeMapping(new(typeof(TimeSpan), new[] { DbType.Time }, convert: static o => o is string s ? Utility.ParseTimeSpan(Encoding.UTF8.GetBytes(s)) : Convert.ChangeType(o, typeof(TimeSpan))));
101+
var typeTime = AddDbTypeMapping(new(typeof(TimeSpan), new[] { DbType.Time }, convert: static o => o is string s ? Utility.ParseTimeSpan(Encoding.UTF8.GetBytes(s)) : Convert.ChangeType(o, typeof(TimeSpan), CultureInfo.InvariantCulture)));
101102
AddColumnTypeMetadata(new("DATETIME", typeDateTime, MySqlDbType.DateTime));
102103
AddColumnTypeMetadata(new("DATE", typeDate, MySqlDbType.Date));
103104
AddColumnTypeMetadata(new("DATE", typeDate, MySqlDbType.Newdate));
@@ -106,7 +107,7 @@ private TypeMapper()
106107
AddColumnTypeMetadata(new("YEAR", typeInt, MySqlDbType.Year));
107108

108109
// guid
109-
var typeGuid = AddDbTypeMapping(new(typeof(Guid), new[] { DbType.Guid }, convert: static o => Guid.Parse(Convert.ToString(o)!)));
110+
var typeGuid = AddDbTypeMapping(new(typeof(Guid), new[] { DbType.Guid }, convert: static o => Guid.Parse(Convert.ToString(o, CultureInfo.InvariantCulture)!)));
110111
AddColumnTypeMetadata(new("CHAR", typeGuid, MySqlDbType.Guid, length: 36, simpleDataTypeName: "CHAR(36)", createFormat: "CHAR(36)"));
111112

112113
// null

src/MySqlConnector/MySqlAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace MySqlConnector
44
/// <see cref="MySqlAttribute"/> represents an attribute that can be sent with a MySQL query.
55
/// </summary>
66
/// <remarks>See <a href="https://dev.mysql.com/doc/refman/8.0/en/query-attributes.html">Query Attributes</a> for information on using query attributes.</remarks>
7+
#pragma warning disable CA1711 // Identifiers should not have incorrect suffix
78
public sealed class MySqlAttribute
9+
#pragma warning restore CA1711 // Identifiers should not have incorrect suffix
810
{
911
/// <summary>
1012
/// Initializes a new <see cref="MySqlAttribute"/>.

src/MySqlConnector/MySqlBatch.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ public void Dispose()
235235
#endif
236236
{
237237
m_isDisposed = true;
238+
#if NET6_0_OR_GREATER
239+
base.Dispose();
240+
#endif
238241
}
239242

240243
internal CommandBehavior CurrentCommandBehavior { get; set; }

src/MySqlConnector/MySqlBatchCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace MySqlConnector;
44

5+
#if !NET6_0_OR_GREATER
6+
#pragma warning disable CA1822 // Mark members as static
7+
#endif
8+
59
public sealed class MySqlBatchCommand :
610
#if NET6_0_OR_GREATER
711
DbBatchCommand,

src/MySqlConnector/MySqlBulkLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private string CreateSql()
273273
sb.AppendFormat(CultureInfo.InvariantCulture, "({0}) ", string.Join(",", Columns));
274274

275275
if (Expressions.Count > 0)
276-
sb.AppendFormat("SET {0}", string.Join(",", Expressions));
276+
sb.AppendFormat(CultureInfo.InvariantCulture, "SET {0}", string.Join(",", Expressions));
277277

278278
sb.Append(';');
279279

src/MySqlConnector/MySqlConnection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
namespace MySqlConnector;
1515

16+
#if !NET6_0_OR_GREATER
17+
#pragma warning disable CA1822 // Mark members as static
18+
#endif
19+
1620
/// <summary>
1721
/// <see cref="MySqlConnection"/> represents a connection to a MySQL database.
1822
/// </summary>

0 commit comments

Comments
 (0)