Skip to content

Commit ba8f1c5

Browse files
authored
Add clang-analyzer-* and clang-diagnostic-* to .clang-tidy (flutter#31291)
1 parent 2a8f388 commit ba8f1c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+191
-56
lines changed

.clang-tidy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# Prefix check with "-" to ignore.
22
Checks: "google-*,\
3+
clang-analyzer-*,\
4+
clang-diagnostic-*,\
35
-google-objc-global-variable-declaration,\
4-
-google-objc-avoid-throwing-exception"
6+
-google-objc-avoid-throwing-exception,\
7+
-clang-analyzer-nullability.NullPassedToNonnull,\
8+
-clang-analyzer-nullability.NullablePassedToNonnull,\
9+
-clang-analyzer-nullability.NullReturnedFromNonnull,\
10+
-clang-analyzer-nullability.NullableReturnedFromNonnull"
511

612
# Only warnings treated as errors are reported
713
# in the "ci/lint.sh" script and pre-push git hook.
814
# Add checks when all warnings are fixed
915
# to prevent new warnings being introduced.
1016
# https://github.com/flutter/flutter/issues/93279
11-
WarningsAsErrors: "clang-analyzer-osx.*,\
17+
WarningsAsErrors: "clang-analyzer-*,\
18+
clang-diagnostic-*,\
1219
google-objc-*,\
1320
google-explicit-constructor"

common/graphics/persistent_cache.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ sk_sp<SkData> ParseBase64(const std::string& input) {
191191
}
192192

193193
size_t PersistentCache::PrecompileKnownSkSLs(GrDirectContext* context) const {
194+
// clang-tidy has trouble reasoning about some of the complicated array and
195+
// pointer-arithmetic code in rapidjson.
196+
// NOLINTNEXTLINE(clang-analyzer-cplusplus.PlacementNew)
194197
auto known_sksls = LoadSkSLs();
195198
// A trace must be present even if no precompilations have been completed.
196199
FML_TRACE_EVENT("flutter", "PersistentCache::PrecompileKnownSkSLs", "count",

display_list/display_list_benchmarks.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void BM_DrawLine(benchmark::State& state,
114114
auto display_list = builder.Build();
115115

116116
// We only want to time the actual rasterization.
117-
for (auto _ : state) {
117+
for ([[maybe_unused]] auto _ : state) {
118118
display_list->RenderTo(canvas);
119119
canvas_provider->GetSurface()->flushAndSubmit(true);
120120
}
@@ -162,7 +162,7 @@ void BM_DrawRect(benchmark::State& state,
162162
auto display_list = builder.Build();
163163

164164
// We only want to time the actual rasterization.
165-
for (auto _ : state) {
165+
for ([[maybe_unused]] auto _ : state) {
166166
display_list->RenderTo(canvas);
167167
canvas_provider->GetSurface()->flushAndSubmit(true);
168168
}
@@ -207,7 +207,7 @@ void BM_DrawOval(benchmark::State& state,
207207
auto display_list = builder.Build();
208208

209209
// We only want to time the actual rasterization.
210-
for (auto _ : state) {
210+
for ([[maybe_unused]] auto _ : state) {
211211
display_list->RenderTo(canvas);
212212
canvas_provider->GetSurface()->flushAndSubmit(true);
213213
}
@@ -254,7 +254,7 @@ void BM_DrawCircle(benchmark::State& state,
254254
auto display_list = builder.Build();
255255

256256
// We only want to time the actual rasterization.
257-
for (auto _ : state) {
257+
for ([[maybe_unused]] auto _ : state) {
258258
display_list->RenderTo(canvas);
259259
canvas_provider->GetSurface()->flushAndSubmit(true);
260260
}
@@ -331,7 +331,7 @@ void BM_DrawRRect(benchmark::State& state,
331331
auto display_list = builder.Build();
332332

333333
// We only want to time the actual rasterization.
334-
for (auto _ : state) {
334+
for ([[maybe_unused]] auto _ : state) {
335335
display_list->RenderTo(canvas);
336336
canvas_provider->GetSurface()->flushAndSubmit(true);
337337
}
@@ -412,7 +412,7 @@ void BM_DrawDRRect(benchmark::State& state,
412412
auto display_list = builder.Build();
413413

414414
// We only want to time the actual rasterization.
415-
for (auto _ : state) {
415+
for ([[maybe_unused]] auto _ : state) {
416416
display_list->RenderTo(canvas);
417417
canvas_provider->GetSurface()->flushAndSubmit(true);
418418
}
@@ -464,7 +464,7 @@ void BM_DrawArc(benchmark::State& state,
464464
auto display_list = builder.Build();
465465

466466
// We only want to time the actual rasterization.
467-
for (auto _ : state) {
467+
for ([[maybe_unused]] auto _ : state) {
468468
display_list->RenderTo(canvas);
469469
canvas_provider->GetSurface()->flushAndSubmit(true);
470470
}
@@ -482,7 +482,7 @@ std::vector<SkPoint> GetPolygonPoints(size_t n, SkPoint center, SkScalar r) {
482482
float angle;
483483
float full_circle = 2.0f * M_PI;
484484
for (size_t i = 0; i < n; i++) {
485-
angle = (full_circle / (float)n) * (float)i;
485+
angle = (full_circle / static_cast<float>(n)) * static_cast<float>(i);
486486
x = center.x() + r * std::cosf(angle);
487487
y = center.y() + r * std::sinf(angle);
488488
points.push_back(SkPoint::Make(x, y));
@@ -666,7 +666,7 @@ void BM_DrawPath(benchmark::State& state,
666666
auto display_list = builder.Build();
667667

668668
// We only want to time the actual rasterization.
669-
for (auto _ : state) {
669+
for ([[maybe_unused]] auto _ : state) {
670670
display_list->RenderTo(canvas);
671671
canvas_provider->GetSurface()->flushAndSubmit(true);
672672
}
@@ -709,12 +709,13 @@ sk_sp<SkVertices> GetTestVertices(SkPoint center,
709709
colors.push_back(SK_ColorCYAN);
710710
for (size_t i = 0; i <= outer_points.size(); i++) {
711711
vertices.push_back(outer_points[i % outer_points.size()]);
712-
if (i % 3 == 0)
712+
if (i % 3 == 0) {
713713
colors.push_back(SK_ColorRED);
714-
else if (i % 3 == 1)
714+
} else if (i % 3 == 1) {
715715
colors.push_back(SK_ColorGREEN);
716-
else
716+
} else {
717717
colors.push_back(SK_ColorBLUE);
718+
}
718719
}
719720
break;
720721
case SkVertices::VertexMode::kTriangles_VertexMode:
@@ -810,7 +811,7 @@ void BM_DrawVertices(benchmark::State& state,
810811
auto display_list = builder.Build();
811812

812813
// We only want to time the actual rasterization.
813-
for (auto _ : state) {
814+
for ([[maybe_unused]] auto _ : state) {
814815
display_list->RenderTo(canvas);
815816
canvas_provider->GetSurface()->flushAndSubmit(true);
816817
}
@@ -913,7 +914,7 @@ void BM_DrawPoints(benchmark::State& state,
913914

914915
auto display_list = builder.Build();
915916

916-
for (auto _ : state) {
917+
for ([[maybe_unused]] auto _ : state) {
917918
display_list->RenderTo(canvas);
918919
canvas_provider->GetSurface()->flushAndSubmit(true);
919920
}
@@ -987,7 +988,7 @@ void BM_DrawImage(benchmark::State& state,
987988

988989
auto display_list = builder.Build();
989990

990-
for (auto _ : state) {
991+
for ([[maybe_unused]] auto _ : state) {
991992
display_list->RenderTo(canvas);
992993
canvas_provider->GetSurface()->flushAndSubmit(true);
993994
}
@@ -1069,7 +1070,7 @@ void BM_DrawImageRect(benchmark::State& state,
10691070

10701071
auto display_list = builder.Build();
10711072

1072-
for (auto _ : state) {
1073+
for ([[maybe_unused]] auto _ : state) {
10731074
display_list->RenderTo(canvas);
10741075
canvas_provider->GetSurface()->flushAndSubmit(true);
10751076
}
@@ -1153,7 +1154,7 @@ void BM_DrawImageNine(benchmark::State& state,
11531154

11541155
auto display_list = builder.Build();
11551156

1156-
for (auto _ : state) {
1157+
for ([[maybe_unused]] auto _ : state) {
11571158
display_list->RenderTo(canvas);
11581159
canvas_provider->GetSurface()->flushAndSubmit(true);
11591160
}
@@ -1226,7 +1227,7 @@ void BM_DrawTextBlob(benchmark::State& state,
12261227

12271228
auto display_list = builder.Build();
12281229

1229-
for (auto _ : state) {
1230+
for ([[maybe_unused]] auto _ : state) {
12301231
display_list->RenderTo(canvas);
12311232
canvas_provider->GetSurface()->flushAndSubmit(true);
12321233
}
@@ -1290,7 +1291,7 @@ void BM_DrawShadow(benchmark::State& state,
12901291
auto display_list = builder.Build();
12911292

12921293
// We only want to time the actual rasterization.
1293-
for (auto _ : state) {
1294+
for ([[maybe_unused]] auto _ : state) {
12941295
display_list->RenderTo(canvas);
12951296
canvas_provider->GetSurface()->flushAndSubmit(true);
12961297
}
@@ -1343,7 +1344,7 @@ void BM_SaveLayer(benchmark::State& state,
13431344
auto display_list = builder.Build();
13441345

13451346
// We only want to time the actual rasterization.
1346-
for (auto _ : state) {
1347+
for ([[maybe_unused]] auto _ : state) {
13471348
display_list->RenderTo(canvas);
13481349
canvas_provider->GetSurface()->flushAndSubmit(true);
13491350
}

flow/paint_utils.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ void DrawCheckerboard(SkCanvas* canvas, const SkRect& rect) {
3737
canvas->save();
3838
canvas->clipRect(rect);
3939

40+
// Secure random number generation isn't needed here.
41+
// NOLINTBEGIN(clang-analyzer-security.insecureAPI.rand)
4042
auto checkerboard_color =
4143
SkColorSetARGB(64, rand() % 256, rand() % 256, rand() % 256);
44+
// NOLINTEND(clang-analyzer-security.insecureAPI.rand)
4245

4346
DrawCheckerboard(canvas, checkerboard_color, 0x00000000, 12);
4447
canvas->restore();

fml/platform/android/jni_util.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ JNIEnv* AttachCurrentThread() {
5252
} else {
5353
args.name = thread_name;
5454
}
55-
jint ret = g_jvm->AttachCurrentThread(&env, &args);
55+
[[maybe_unused]] jint ret = g_jvm->AttachCurrentThread(&env, &args);
5656
FML_DCHECK(JNI_OK == ret);
5757

5858
FML_DCHECK(tls_jni_detach.get() == nullptr);
@@ -182,9 +182,10 @@ ScopedJavaLocalRef<jobjectArray> VectorToBufferArray(
182182
env->NewObjectArray(vector.size(), byte_buffer_clazz.obj(), NULL);
183183
ASSERT_NO_EXCEPTION();
184184
for (size_t i = 0; i < vector.size(); ++i) {
185+
uint8_t* data = const_cast<uint8_t*>(vector[i].data());
185186
ScopedJavaLocalRef<jobject> item(
186-
env,
187-
env->NewDirectByteBuffer((void*)(vector[i].data()), vector[i].size()));
187+
env, env->NewDirectByteBuffer(reinterpret_cast<void*>(data),
188+
vector[i].size()));
188189
env->SetObjectArrayElement(java_array, i, item.obj());
189190
}
190191
return ScopedJavaLocalRef<jobjectArray>(env, java_array);
@@ -195,16 +196,18 @@ bool HasException(JNIEnv* env) {
195196
}
196197

197198
bool ClearException(JNIEnv* env) {
198-
if (!HasException(env))
199+
if (!HasException(env)) {
199200
return false;
201+
}
200202
env->ExceptionDescribe();
201203
env->ExceptionClear();
202204
return true;
203205
}
204206

205207
bool CheckException(JNIEnv* env) {
206-
if (!HasException(env))
208+
if (!HasException(env)) {
207209
return true;
210+
}
208211

209212
jthrowable exception = env->ExceptionOccurred();
210213
env->ExceptionClear();

fml/platform/android/message_loop_android.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void MessageLoopAndroid::Terminate() {
8383
}
8484

8585
void MessageLoopAndroid::WakeUp(fml::TimePoint time_point) {
86-
bool result = TimerRearm(timer_fd_.get(), time_point);
86+
[[maybe_unused]] bool result = TimerRearm(timer_fd_.get(), time_point);
8787
FML_DCHECK(result);
8888
}
8989

fml/platform/android/scoped_java_ref.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ namespace jni {
1313
static const int kDefaultLocalFrameCapacity = 16;
1414

1515
ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env) : env_(env) {
16-
int failed = env_->PushLocalFrame(kDefaultLocalFrameCapacity);
16+
[[maybe_unused]] int failed =
17+
env_->PushLocalFrame(kDefaultLocalFrameCapacity);
1718
FML_DCHECK(!failed);
1819
}
1920

2021
ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env, int capacity)
2122
: env_(env) {
22-
int failed = env_->PushLocalFrame(capacity);
23+
[[maybe_unused]] int failed = env_->PushLocalFrame(capacity);
2324
FML_DCHECK(!failed);
2425
}
2526

@@ -43,10 +44,12 @@ JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) {
4344
} else {
4445
FML_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread.
4546
}
46-
if (obj)
47+
if (obj) {
4748
obj = env->NewLocalRef(obj);
48-
if (obj_)
49+
}
50+
if (obj_) {
4951
env->DeleteLocalRef(obj_);
52+
}
5053
obj_ = obj;
5154
return env;
5255
}
@@ -57,10 +60,12 @@ void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) {
5760
} else {
5861
FML_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread.
5962
}
60-
if (obj)
63+
if (obj) {
6164
obj = env->NewGlobalRef(obj);
62-
if (obj_)
65+
}
66+
if (obj_) {
6367
env->DeleteGlobalRef(obj_);
68+
}
6469
obj_ = obj;
6570
}
6671

fml/synchronization/waitable_event_unittest.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "flutter/fml/macros.h"
1616
#include "gtest/gtest.h"
1717

18+
// rand() is only used for tests in this file.
19+
// NOLINTBEGIN(clang-analyzer-security.insecureAPI.rand)
20+
1821
namespace fml {
1922
namespace {
2023

@@ -183,3 +186,5 @@ TEST(ManualResetWaitableEventTest, SignalMultiple) {
183186

184187
} // namespace
185188
} // namespace fml
189+
190+
// NOLINTEND(clang-analyzer-security.insecureAPI.rand)

lib/ui/compositing/scene_builder_unittests.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include "flutter/shell/common/thread_host.h"
1414
#include "flutter/testing/testing.h"
1515

16+
// CREATE_NATIVE_ENTRY is leaky by design
17+
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
18+
1619
namespace flutter {
1720
namespace testing {
1821

@@ -156,3 +159,5 @@ TEST_F(ShellTest, EngineLayerDisposeReleasesReference) {
156159

157160
} // namespace testing
158161
} // namespace flutter
162+
163+
// NOLINTEND(clang-analyzer-core.StackAddressEscape)

lib/ui/hooks_unittests.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "flutter/testing/testing.h"
1313
#include "third_party/dart/runtime/include/dart_api.h"
1414

15+
// CREATE_NATIVE_ENTRY is leaky by design
16+
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
17+
1518
namespace flutter {
1619
namespace testing {
1720

@@ -82,3 +85,5 @@ TEST_F(HooksTest, HooksUnitTests) {
8285

8386
} // namespace testing
8487
} // namespace flutter
88+
89+
// NOLINTEND(clang-analyzer-core.StackAddressEscape)

0 commit comments

Comments
 (0)