Skip to content

Commit c882fe4

Browse files
committed
Add official BannedApiAnalyzers and ban some string parsing methods
1 parent 8462537 commit c882fe4

File tree

9 files changed

+35
-19
lines changed

9 files changed

+35
-19
lines changed

.global.editorconfig.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ dotnet_diagnostic.MEN015.severity = silent
376376
# Use object-oriented methods instead of top-level statements
377377
dotnet_diagnostic.MEN016.severity = silent
378378

379+
## Microsoft.CodeAnalysis.BannedApiAnalyzers rules
380+
381+
# Do not use banned APIs
382+
dotnet_diagnostic.RS0030.severity = error
383+
379384
## StyleCop spacing rules
380385

381386
# Keywords should be spaced correctly

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageVersion Include="Menees.Analyzers" Version="3.2.2" />
1010
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.159" />
1111
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
12+
<PackageVersion Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
1213
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
1314
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
1415
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
M:System.Convert.ToByte(System.String);use byte.{Try,}Parse
2+
M:System.Convert.ToDecimal(System.String);use decimal.{Try,}Parse
3+
M:System.Convert.ToDouble(System.String);use double.{Try,}Parse
4+
M:System.Convert.ToInt16(System.String);use short.{Try,}Parse
5+
M:System.Convert.ToInt32(System.String);use int.{Try,}Parse
6+
M:System.Convert.ToInt64(System.String);use long.{Try,}Parse
7+
M:System.Convert.ToSByte(System.String);use sbyte.{Try,}Parse
8+
M:System.Convert.ToSingle(System.String);use float.{Try,}Parse
9+
M:System.Convert.ToUInt16(System.String);use ushort.{Try,}Parse
10+
M:System.Convert.ToUInt32(System.String);use uint.{Try,}Parse
11+
M:System.Convert.ToUInt64(System.String);use ulong.{Try,}Parse

src/BizHawk.Client.Common/movie/BasicMovieInfo.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public TimeSpan TimeLength
4848

4949
if (Header.TryGetValue(HeaderKeys.CycleCount, out var numCyclesStr) && Header.TryGetValue(HeaderKeys.ClockRate, out var clockRateStr))
5050
{
51-
var numCycles = Convert.ToUInt64(numCyclesStr);
52-
var clockRate = Convert.ToDouble(clockRateStr, CultureInfo.InvariantCulture);
51+
var numCycles = ulong.Parse(numCyclesStr);
52+
var clockRate = double.Parse(clockRateStr, CultureInfo.InvariantCulture);
5353
numSeconds = numCycles / clockRate;
5454
}
5555
else
@@ -68,8 +68,10 @@ public double FrameRate
6868
{
6969
if (SystemID == VSystemID.Raw.Arcade && Header.TryGetValue(HeaderKeys.VsyncAttoseconds, out var vsyncAttoStr))
7070
{
71-
const decimal attosInSec = 1000000000000000000;
72-
return (double)(attosInSec / Convert.ToUInt64(vsyncAttoStr));
71+
const decimal attosInSec = 1_000_000_000_000_000_000.0M;
72+
var m = attosInSec;
73+
m /= ulong.Parse(vsyncAttoStr);
74+
return checked((double) m);
7375
}
7476
else
7577
{

src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ private void MovieView_SelectedIndexChanged(object sender, EventArgs e)
422422
}
423423
break;
424424
case HeaderKeys.VsyncAttoseconds:
425-
if (_emulator is MAME mame && mame.VsyncAttoseconds != Convert.ToInt64(v))
425+
if (_emulator is MAME mame && mame.VsyncAttoseconds != long.Parse(v))
426426
{
427427
item.BackColor = Color.Pink;
428428
item.ToolTipText = $"Expected: {v}\n Actual: {mame.VsyncAttoseconds}";

src/BizHawk.Client.EmuHawk/tools/TAStudio/PatternsForm.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void ValueNum_ValueChanged(object sender, EventArgs e)
110110
return;
111111
}
112112

113-
_values[PatternList.SelectedIndex] = ValueNum.Value.ToString(NumberFormatInfo.InvariantInfo);
113+
_values[PatternList.SelectedIndex] = unchecked((int) ValueNum.Value).ToString(NumberFormatInfo.InvariantInfo);
114114
UpdatePattern();
115115
UpdateDisplay();
116116
}
@@ -217,7 +217,7 @@ private void UpdateDisplay()
217217
}
218218

219219
LagBox.Checked = _tastudio.AxisPatterns[index].SkipsLag;
220-
ValueNum.Value = Convert.ToDecimal(_values[PatternList.SelectedIndex]);
220+
ValueNum.Value = int.Parse(_values[PatternList.SelectedIndex]);
221221
CountNum.Value = _counts[PatternList.SelectedIndex];
222222
}
223223
}
@@ -267,7 +267,7 @@ private void UpdatePattern()
267267
{
268268
for (int c = 0; c < _counts[i]; c++)
269269
{
270-
p.Add((int) Convert.ToSingle(_values[i]));
270+
p.Add(int.Parse(_values[i]));
271271
}
272272
}
273273

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,14 +1323,7 @@ public void EditAnalogProgrammatically(KeyEventArgs e)
13231323
}
13241324

13251325
_axisTypedValue = _axisTypedValue.Substring(startIndex: 0, length: _axisTypedValue.Length - 1); // drop last char
1326-
if (_axisTypedValue == "" || _axisTypedValue == "-")
1327-
{
1328-
value = 0f;
1329-
}
1330-
else
1331-
{
1332-
value = Convert.ToSingle(_axisTypedValue);
1333-
}
1326+
if (!float.TryParse(_axisTypedValue, out value)) value = 0.0f;
13341327
}
13351328
else if (e.KeyCode == Keys.Enter)
13361329
{

src/BizHawk.Emulation.Cores/CPUs/FairchildF8/F3850.Registers.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ public ushort RegIRQV
241241
}
242242
}
243243

244+
private const string PFX_SCRATCHPAD_REG = "SPR";
245+
244246
public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
245247
{
246248
var res = new Dictionary<string, RegisterValue>
@@ -266,17 +268,17 @@ public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
266268

267269
for (int i = 0; i < 64; i++)
268270
{
269-
res.Add("SPR" + i, Regs[i]);
271+
res.Add(PFX_SCRATCHPAD_REG + i, Regs[i]);
270272
}
271273

272274
return res;
273275
}
274276

275277
public void SetCpuRegister(string register, int value)
276278
{
277-
if (register.StartsWithOrdinal("SPR"))
279+
if (register.StartsWithOrdinal(PFX_SCRATCHPAD_REG))
278280
{
279-
var reg = Convert.ToInt32(register.Replace("SPR", ""));
281+
var reg = int.Parse(register.Substring(startIndex: PFX_SCRATCHPAD_REG.Length));
280282

281283
if (reg > 63)
282284
{

src/MainSlnCommon.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
1919
</PropertyGroup>
2020
<ItemGroup>
21+
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" PrivateAssets="all" />
2122
<Analyzer Include="$(MSBuildProjectDirectory)/../../References/BizHawk.SrcGen.ReflectionCache.dll" />
23+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)/BannedSymbols.BannedApiAnalyzers.txt" />
2224
<Using Include="System" />
2325
</ItemGroup>
2426
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">

0 commit comments

Comments
 (0)