Skip to content

in modifier for b2Polygon#86

Merged
ikpil merged 2 commits into
ikpil:mainfrom
Happypig375:in_polygon
Jun 10, 2026
Merged

in modifier for b2Polygon#86
ikpil merged 2 commits into
ikpil:mainfrom
Happypig375:in_polygon

Conversation

@Happypig375

Copy link
Copy Markdown
Contributor

I did not encounter the warning as specified in #57 (comment).

All warnings from Visual Studio are as follows:

Severity	Code	Description	Project	File	Line	Suppression State	Details
Warning (active)	CA2265	Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)	Box2D.NET (net10.0), Box2D.NET (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2Distances.cs	619		
Warning (active)	CS0414	The field 'B2BoardPhases.b2_dynamicStats' is assigned but its value is never used	Box2D.NET (net10.0), Box2D.NET (net8.0), Box2D.NET (net9.0), Box2D.NET (netstandard2.1)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2BoardPhases.cs	27		
Warning (active)	CS0414	The field 'B2BoardPhases.b2_kinematicStats' is assigned but its value is never used	Box2D.NET (net10.0), Box2D.NET (net8.0), Box2D.NET (net9.0), Box2D.NET (netstandard2.1)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2BoardPhases.cs	28		
Warning (active)	CS0414	The field 'B2BoardPhases.b2_staticStats' is assigned but its value is never used	Box2D.NET (net10.0), Box2D.NET (net8.0), Box2D.NET (net9.0), Box2D.NET (netstandard2.1)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2BoardPhases.cs	29		
Warning (active)	CS0162	Unreachable code detected	Box2D.NET (net10.0), Box2D.NET (net8.0), Box2D.NET (net9.0), Box2D.NET (netstandard2.1)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2Solvers.cs	52		
Warning (active)	CS0162	Unreachable code detected	Box2D.NET (net10.0), Box2D.NET (net8.0), Box2D.NET (net9.0), Box2D.NET (netstandard2.1)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2Solvers.cs	60		
Warning (active)	CA2265	Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)	Box2D.NET (net10.0), Box2D.NET (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2Distances.cs	497		
Warning (active)	CA2265	Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)	Box2D.NET (net10.0), Box2D.NET (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET\B2Distances.cs	557		
Warning (active)	CS0219	The variable 'fileBufferCapacity' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Graphics\FontsV1.cs	65		
Warning (active)	CS0219	The variable 'pw' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Graphics\FontsV1.cs	152		
Warning (active)	CS0219	The variable 'ph' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Graphics\FontsV1.cs	153		
Warning (active)	CS0219	The variable 'loop' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Primitives\TaskScheduler.cs	40		
Warning (active)	CS0169	The field 'BenchmarkCast.m_updateType' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Benchmarks\BenchmarkCast.cs	35		
Warning (active)	CS0169	The field 'BenchmarkSleep.m_awake' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Benchmarks\BenchmarkSleep.cs	27		
Warning (active)	CS0169	The field 'Mover.m_deltaX' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Characters\Mover.cs	88		
Warning (active)	CS0169	The field 'Mover.m_deltaY' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Characters\Mover.cs	89		
Warning (active)	CS0219	The variable 'noWalkSteer' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Characters\Mover.cs	310		
Warning (active)	CS0169	The field 'OverlapWorld.m_transform' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Collisions\OverlapWorld.cs	48		
Warning (active)	CS0169	The field 'OverlapWorld.m_basePosition' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Collisions\OverlapWorld.cs	53		
Warning (active)	CS0414	The field 'ShapeCast.m_drawSimplex' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Collisions\ShapeCast.cs	53		
Warning (active)	CS0649	Field 'Doohickey.m_axleId1' is never assigned to, and will always have its default value	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Doohickey.cs	21		
Warning (active)	CS0649	Field 'Doohickey.m_axleId2' is never assigned to, and will always have its default value	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Doohickey.cs	22		
Warning (active)	CS0649	Field 'Doohickey.m_sliderId' is never assigned to, and will always have its default value	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Doohickey.cs	23		
Warning (active)	CS0649	Field 'MotorJoint._transform' is never assigned to, and will always have its default value	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Joints\MotorJoint.cs	35		
Warning (active)	CS0169	The field 'ModifyGeometry.m_shape' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Shapes\ModifyGeometry.cs	24		
Warning (active)	CS0169	The field 'CapsuleStack.Event.indexA' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Stackings\CapsuleStack.cs	18		
Warning (active)	CS0169	The field 'CapsuleStack.Event.indexB' is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Stackings\CapsuleStack.cs	18		
Warning (active)	CS0414	The field 'CircleImpulse.m_gravity' is assigned but its value is never used	Box2D.NET.Samples (net10.0), Box2D.NET.Samples (net8.0), Box2D.NET.Samples (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\src\Box2D.NET.Samples\Samples\Stackings\CircleImpulse.cs	33		
Warning (active)	CS0219	The variable 'index' is assigned but its value is never used	Box2D.NET.Test (net10.0), Box2D.NET.Test (net8.0), Box2D.NET.Test (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\test\Box2D.NET.Test\B2DeterminismTest.cs	59		
Warning (active)	CS1718	Comparison made to same variable; did you mean to compare something else?	Box2D.NET.Test (net10.0), Box2D.NET.Test (net8.0), Box2D.NET.Test (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\test\Box2D.NET.Test\B2Vec2Test.cs	183		
Warning (active)	CS1718	Comparison made to same variable; did you mean to compare something else?	Box2D.NET.Test (net10.0), Box2D.NET.Test (net8.0), Box2D.NET.Test (net9.0)	C:\Users\hadri\source\repos\Box2D.NET\test\Box2D.NET.Test\B2Vec2Test.cs	184		

@Happypig375

Copy link
Copy Markdown
Contributor Author

@ikpil

@ikpil

ikpil commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Thanks for pointing this out.

I checked the warning, and the issue is not AsSpan() itself, but where it is being called.

In this case b2CollidePolygonAndCircle receives polygonA as an in B2Polygon parameter. Since B2Polygon.vertices and B2Polygon.normals are B2FixedArray8<T> mutable structs, calling the non-readonly AsSpan() method on them may cause a defensive copy. Rider reports this as a warning because the returned span may point to a temporary copy instead of the original struct storage.

For this specific code path we only need read-only access, so I will add a dedicated AsReadOnlySpan() method to the fixed array types and use that from read-only contexts.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<T> AsReadOnlySpan()
{
    return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
}

There is one additional compatibility detail: the project still targets netstandard2.1, and Unsafe is not available there unless the package is referenced explicitly. To keep netstandard2.1 compatibility, I will add the following package reference:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
  <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
</ItemGroup>

So the fix will be:

Add AsReadOnlySpan() for read-only fixed-array access.
Replace read-only AsSpan() calls in in/readonly contexts with AsReadOnlySpan().
Add the System.Runtime.CompilerServices.Unsafe package reference for netstandard2.1.
This should remove the Rider warning while preserving the existing behavior and target framework compatibility.

@ikpil

ikpil commented Jun 9, 2026

Copy link
Copy Markdown
Owner

The prerequisite work has been completed and merged.
For now, I've added a compile-time safeguard that causes a compilation error when AsSpan() is used with in parameters.
I'll take another look at your changes tomorrow.

@ikpil ikpil force-pushed the in_polygon branch 2 times, most recently from 636f35a to 56c9c86 Compare June 10, 2026 15:48
@ikpil ikpil merged commit 3627cf4 into ikpil:main Jun 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants