Skip to content

Commit 0d017c9

Browse files
paulirwinclaude
andcommitted
Add Sample project entries for LuceneDev4000-4002
Demonstrates each NoInlining diagnostic firing in the sample project: 4000 on an interface and an abstract method, 4001 on an empty-bodied method, and 4002 on a method referenced by the 2-arg StackTraceHelper.DoesStackTraceContainMethod overload (with a local stub mirroring the real type so the sample compiles standalone). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3fd68b3 commit 0d017c9

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
using System.Runtime.CompilerServices;
21+
22+
namespace Lucene.Net.CodeAnalysis.Dev.Sample.LuceneDev4xxx;
23+
24+
public interface ILuceneDev4000Sample
25+
{
26+
// Triggers LuceneDev4000 (Warning): MethodImpl is not inherited, so NoInlining
27+
// on an interface member has no effect on the implementation.
28+
[MethodImpl(MethodImplOptions.NoInlining)]
29+
void DoWork();
30+
}
31+
32+
public abstract class LuceneDev4000Sample
33+
{
34+
// Triggers LuceneDev4000 (Warning): same reason — abstract methods are not
35+
// bodies that the JIT can inline.
36+
[MethodImpl(MethodImplOptions.NoInlining)]
37+
public abstract void DoWork();
38+
}
39+
40+
public class LuceneDev4001Sample
41+
{
42+
// Triggers LuceneDev4001 (Warning): empty-bodied methods cannot appear above
43+
// any frame in a stack trace, so preventing inlining provides no benefit and
44+
// only harms performance.
45+
[MethodImpl(MethodImplOptions.NoInlining)]
46+
public void EmptyMethod()
47+
{
48+
}
49+
50+
// No diagnostic: regular method with a non-empty body.
51+
[MethodImpl(MethodImplOptions.NoInlining)]
52+
public int RealWork()
53+
{
54+
return 42;
55+
}
56+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
using System.Runtime.CompilerServices;
21+
using Lucene.Net.Support.ExceptionHandling;
22+
23+
// Stub mirroring Lucene.Net.Support.ExceptionHandling.StackTraceHelper so the
24+
// sample compiles in isolation. The analyzer matches by full type name.
25+
// Suppress LuceneDev1005 — that rule flags real public types in Lucene.Net.Support;
26+
// this is just a local stand-in for the sample.
27+
#pragma warning disable LuceneDev1005
28+
namespace Lucene.Net.Support.ExceptionHandling
29+
{
30+
public static class StackTraceHelper
31+
{
32+
public static bool DoesStackTraceContainMethod(string methodName) => false;
33+
public static bool DoesStackTraceContainMethod(string className, string methodName) => false;
34+
}
35+
}
36+
#pragma warning restore LuceneDev1005
37+
38+
namespace Lucene.Net.CodeAnalysis.Dev.Sample.LuceneDev4xxx
39+
{
40+
public class LuceneDev4002_TargetWithoutNoInlining
41+
{
42+
// Triggers LuceneDev4002 (Warning): this method is referenced by the
43+
// 2-argument StackTraceHelper.DoesStackTraceContainMethod overload below
44+
// but is missing [MethodImpl(MethodImplOptions.NoInlining)]. The JIT may
45+
// inline it out of the stack trace, silently breaking the check.
46+
public void Merge()
47+
{
48+
System.Console.WriteLine(1 + 2);
49+
}
50+
}
51+
52+
public class LuceneDev4002_TargetWithNoInlining
53+
{
54+
// No diagnostic: the attribute is already applied.
55+
[MethodImpl(MethodImplOptions.NoInlining)]
56+
public void Merge()
57+
{
58+
System.Console.WriteLine(1 + 2);
59+
}
60+
}
61+
62+
public class LuceneDev4002_Caller
63+
{
64+
public void Check()
65+
{
66+
// The 2-argument overload triggers LuceneDev4002 on the referenced method.
67+
if (StackTraceHelper.DoesStackTraceContainMethod(
68+
nameof(LuceneDev4002_TargetWithoutNoInlining),
69+
nameof(LuceneDev4002_TargetWithoutNoInlining.Merge)))
70+
{
71+
}
72+
73+
// No diagnostic for this target — already has NoInlining.
74+
if (StackTraceHelper.DoesStackTraceContainMethod(
75+
nameof(LuceneDev4002_TargetWithNoInlining),
76+
nameof(LuceneDev4002_TargetWithNoInlining.Merge)))
77+
{
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)