Skip to content

Commit 6c95eb2

Browse files
authored
Fix: remove many compiler warnings (#1461) (#1469)
1 parent 9ba1913 commit 6c95eb2

File tree

10 files changed

+88
-83
lines changed

10 files changed

+88
-83
lines changed

src/Neo.Compiler.CSharp/ContractInterfaceGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static string GenerateInterface(string contractName, ContractManifest man
7575
sourceCode.WriteLine($" [Safe]");
7676
}
7777

78-
if (property.setter != null)
78+
if (property.setter is not null)
7979
{
8080
sourceCode.WriteLine($" {returnType} {propertyName} {{ get; set; }}");
8181
}
@@ -157,7 +157,7 @@ private static (ContractMethodDescriptor[] methods, (ContractMethodDescriptor ge
157157
string propertyName = getter.Name.Substring(4);
158158
var setter = methods.FirstOrDefault(m => m.Name == $"set_{propertyName}");
159159

160-
if (setter != null)
160+
if (setter is not null)
161161
remainingMethods.Remove(setter);
162162

163163
properties.Add((getter, setter));

src/Neo.Compiler.CSharp/Manifest/ContractManifestExtension.Nep11.cs

Lines changed: 50 additions & 52 deletions
Large diffs are not rendered by default.

src/Neo.Compiler.CSharp/Manifest/ContractManifestExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static System.Collections.Generic.List<CompilationException>
2424
CheckNep29Compliant(this ContractManifest manifest)
2525
{
2626
var deployMethod = manifest.Abi.GetMethod("_deploy", 2);
27-
var deployValid = deployMethod != null &&
27+
var deployValid = deployMethod is not null &&
2828
deployMethod.ReturnType == ContractParameterType.Void &&
2929
deployMethod.Parameters.Length == 2 &&
3030
deployMethod.Parameters[0].Type == ContractParameterType.Any &&
@@ -40,7 +40,7 @@ private static System.Collections.Generic.List<CompilationException>
4040
CheckNep30Compliant(this ContractManifest manifest)
4141
{
4242
var verifyMethod = manifest.Abi.GetMethod("verify", -1);
43-
var verifyValid = verifyMethod != null && verifyMethod.Safe &&
43+
var verifyValid = verifyMethod is not null && verifyMethod.Safe &&
4444
verifyMethod.ReturnType == ContractParameterType.Boolean;
4545

4646
System.Collections.Generic.List<CompilationException> errors = [];

src/Neo.Compiler.CSharp/Manifest/ContractManifestExtensions.Nep17.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,84 +31,84 @@ private static System.Collections.Generic.List<CompilationException>
3131
var transferMethod = manifest.Abi.GetMethod("transfer", 4);
3232

3333
// Check symbol method
34-
if (symbolMethod == null)
34+
if (symbolMethod is null)
3535
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
3636
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: symbol, it is not found in the ABI"));
3737

3838
if (symbolMethod is { Safe: false })
3939
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
4040
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: symbol, it is not safe, you should add a 'Safe' attribute to the symbol method"));
4141

42-
if (symbolMethod != null && symbolMethod.ReturnType != ContractParameterType.String)
42+
if (symbolMethod is not null && symbolMethod.ReturnType != ContractParameterType.String)
4343
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
4444
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: symbol, it's return type is not a String"));
4545

4646
// Check decimals method
47-
if (decimalsMethod == null)
47+
if (decimalsMethod is null)
4848
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
4949
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: decimals, it is not found in the ABI"));
5050

5151
if (decimalsMethod is { Safe: false })
5252
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
5353
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: decimals, it is not safe, you should add a 'Safe' attribute to the decimals method"));
5454

55-
if (decimalsMethod != null && decimalsMethod.ReturnType != ContractParameterType.Integer)
55+
if (decimalsMethod is not null && decimalsMethod.ReturnType != ContractParameterType.Integer)
5656
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
5757
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: decimals, it's return type is not an Integer"));
5858

5959
// Check totalSupply method
60-
if (totalSupplyMethod == null)
60+
if (totalSupplyMethod is null)
6161
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
6262
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: totalSupply, it is not found in the ABI"));
6363

6464
if (totalSupplyMethod is { Safe: false })
6565
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
6666
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: totalSupply, it is not safe, you should add a 'Safe' attribute to the totalSupply method"));
6767

68-
if (totalSupplyMethod != null && totalSupplyMethod.ReturnType != ContractParameterType.Integer)
68+
if (totalSupplyMethod is not null && totalSupplyMethod.ReturnType != ContractParameterType.Integer)
6969
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
7070
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: totalSupply, it's return type is not an Integer"));
7171

7272
// Check balanceOf method
73-
if (balanceOfMethod == null)
73+
if (balanceOfMethod is null)
7474
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
7575
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf, it is not found in the ABI"));
7676

7777
if (balanceOfMethod is { Safe: false })
7878
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
7979
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf, it is not safe, you should add a 'Safe' attribute to the balanceOf method"));
8080

81-
if (balanceOfMethod != null && balanceOfMethod.ReturnType != ContractParameterType.Integer)
81+
if (balanceOfMethod is not null && balanceOfMethod.ReturnType != ContractParameterType.Integer)
8282
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
8383
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf, it's return type is not an Integer"));
8484

85-
if (balanceOfMethod != null && balanceOfMethod.Parameters.Length != 1)
85+
if (balanceOfMethod is not null && balanceOfMethod.Parameters.Length != 1)
8686
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
8787
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf, it's parameters length is not 1"));
8888

89-
if (balanceOfMethod != null && balanceOfMethod.Parameters[0].Type != ContractParameterType.Hash160)
89+
if (balanceOfMethod is not null && balanceOfMethod.Parameters[0].Type != ContractParameterType.Hash160)
9090
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
9191
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf, it's parameter type is not a Hash160"));
9292

9393
// Check transfer method
94-
if (transferMethod == null)
94+
if (transferMethod is null)
9595
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
9696
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: transfer, it is not found in the ABI"));
9797

9898
// Note: transfer method should NOT be safe as per NEP-17 standard
99-
if (transferMethod != null && transferMethod.Safe)
99+
if (transferMethod is not null && transferMethod.Safe)
100100
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
101101
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: transfer, it should not be marked as Safe"));
102102

103-
if (transferMethod != null && transferMethod.ReturnType != ContractParameterType.Boolean)
103+
if (transferMethod is not null && transferMethod.ReturnType != ContractParameterType.Boolean)
104104
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
105105
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: transfer, it's return type is not a Boolean"));
106106

107-
if (transferMethod != null && transferMethod.Parameters.Length != 4)
107+
if (transferMethod is not null && transferMethod.Parameters.Length != 4)
108108
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
109109
$"Incomplete or unsafe NEP standard {NepStandard.Nep17.ToStandard()} implementation: transfer, it's parameters length is not 4"));
110110

111-
if (transferMethod != null && transferMethod.Parameters.Length == 4)
111+
if (transferMethod is not null && transferMethod.Parameters.Length == 4)
112112
{
113113
if (transferMethod.Parameters[0].Type != ContractParameterType.Hash160)
114114
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
@@ -128,12 +128,12 @@ private static System.Collections.Generic.List<CompilationException>
128128
}
129129

130130
// Check Transfer event
131-
var transferEvent = manifest.Abi.Events.FirstOrDefault(e =>
132-
e.Name == "Transfer");
133-
134-
if (transferEvent == null)
131+
var transferEvent = manifest.Abi.Events.FirstOrDefault(e => e.Name == "Transfer");
132+
if (transferEvent is null)
133+
{
135134
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
136135
$"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: Transfer event is not found in the ABI"));
136+
}
137137
else
138138
{
139139
if (transferEvent.Parameters.Length != 3)

src/Neo.Compiler.CSharp/Manifest/ContractManifestExtensions.Nep24.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static System.Collections.Generic.List<CompilationException>
2626
var royaltyInfoMethod = manifest.Abi.GetMethod("royaltyInfo", 3);
2727

2828
// Check if method exists
29-
if (royaltyInfoMethod == null)
29+
if (royaltyInfoMethod is null)
3030
{
3131
errors.Add(new CompilationException(DiagnosticId.IncorrectNEPStandard,
3232
$"Incomplete or unsafe NEP standard {NepStandard.Nep24.ToStandard()} implementation: royaltyInfo, it is not found in the ABI"));

src/Neo.SmartContract.Framework/Services/Runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static extern UInt160 EntryScriptHash
8181
}
8282

8383
/// <summary>
84-
/// Gets the unixtimestamp in seconds of the current block.
84+
/// Gets the unixtimestamp in milliseconds of the current block.
8585
/// </summary>
8686
public static extern ulong Time
8787
{

src/Neo.SmartContract.Testing/Extensions/ArtifactExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private static (ContractMethodDescriptor[] methods, (ContractMethodDescriptor ge
289289
properties.Add((getter, setter));
290290
methodList.Remove(getter);
291291

292-
if (setter != null)
292+
if (setter is not null)
293293
{
294294
methodList.Remove(setter);
295295
}
@@ -431,7 +431,7 @@ private static string CreateSourceMethodFromManifest(ContractMethodDescriptor me
431431
if (debugInfo != null && nefFile != null)
432432
{
433433
var debugMethod = Disassembler.CSharp.Disassembler.GetMethod(method, debugInfo);
434-
if (debugMethod != null)
434+
if (debugMethod is not null)
435435
{
436436
var (start, end) = Disassembler.CSharp.Disassembler.GetMethodStartEndAddress(debugMethod);
437437
var instructions = Disassembler.CSharp.Disassembler.ConvertMethodToInstructions(nefFile, start, end);
@@ -530,7 +530,7 @@ private static string BuildExtraInformation(JObject sequenceV2)
530530

531531
foreach (var entry in compilerArray)
532532
{
533-
if (entry == null) continue;
533+
if (entry is null) continue;
534534
if (first) first = false;
535535
else builder.Append(',');
536536
builder.Append($"{entry["file"]?.AsString()}:{entry["line"]}({entry["method"]?.AsString()})");

src/Neo.SmartContract.Testing/TestEngine.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ public TestEngine(EngineStorage storage, ProtocolSettings settings, bool initial
301301
if (Storage.IsInitialized)
302302
{
303303
var currentHash = NativeContract.Ledger.CurrentHash(Storage.Snapshot);
304-
PersistingBlock = new PersistingBlock(this, NativeContract.Ledger.GetBlock(Storage.Snapshot, currentHash));
304+
var block = NativeContract.Ledger.GetBlock(Storage.Snapshot, currentHash);
305+
if (block is null) throw new InvalidOperationException($"Can't get the current block {currentHash}");
306+
307+
PersistingBlock = new PersistingBlock(this, block);
305308
}
306309
else
307310
{

src/Neo.SmartContract.Testing/TestingApplicationEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static TestingApplicationEngine()
4545
{
4646
Name = TestingSyscall.Name,
4747
Handler = typeof(TestingApplicationEngine).GetMethod(nameof(InvokeTestingSyscall),
48-
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic),
48+
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!,
4949
FixedPrice = 0,
5050
RequiredCallFlags = CallFlags.None,
5151
};

tests/Neo.Compiler.CSharp.UnitTests/UnitTest_NativeContracts.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,15 @@ public void Test_GAS()
7676
public void Test_Ledger()
7777
{
7878
var genesisBlock = NativeContract.Ledger.GetBlock(Engine.Storage.Snapshot, 0);
79+
Assert.IsNotNull(genesisBlock);
7980
Assert.AreEqual(NativeContract.Ledger.Hash, Contract.LedgerHash());
8081
AssertGasConsumed(984270);
8182
Assert.AreEqual(0, Contract.LedgerCurrentIndex());
8283
AssertGasConsumed(2950140);
83-
Assert.AreEqual(genesisBlock.Hash, Contract.LedgerCurrentHash());
84+
85+
var currentHash = Contract.LedgerCurrentHash();
86+
Assert.IsNotNull(currentHash);
87+
Assert.AreEqual(genesisBlock.Hash, currentHash);
8488
AssertGasConsumed(2950140);
8589
}
8690
}

0 commit comments

Comments
 (0)