Skip to content

Commit 9ce360b

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Enable Clang Tidy (facebook#43299)
Summary: X-link: facebook/litho#976 X-link: facebook/yoga#1586 Pull Request resolved: facebook#43299 Add the React Clang Tidy config to Yoga, run the auto fixes, and make some manual mechanical tweaks. Notably, the automatic changes to the infra for generating a Yoga tree from JSON capture make it 70% faster. Before: {F1463947076} After: {F1463946802} This also cleans up all the no-op shallow const parameters in headers. {F1463943386} Not all checks are available in all environments, but that is okay, as Clang Tidy will gracefully skip them. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D54461054 fbshipit-source-id: dbd2d9ce51afd3174d1f2c6d439fa7d08baff46f
1 parent f5c8bc1 commit 9ce360b

25 files changed

+240
-238
lines changed

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedGlobalRef.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ScopedGlobalRef {
6262
*
6363
* @param globalRef the global reference to wrap. Can be NULL.
6464
*/
65-
ScopedGlobalRef(T globalRef) : mGlobalRef(globalRef) {}
65+
explicit ScopedGlobalRef(T globalRef) : mGlobalRef(globalRef) {}
6666

6767
/**
6868
* Equivalent to ScopedGlobalRef(NULL)
@@ -72,12 +72,12 @@ class ScopedGlobalRef {
7272
/**
7373
* Move construction is allowed.
7474
*/
75-
ScopedGlobalRef(ScopedGlobalRef&& s) : mGlobalRef(s.release()) {}
75+
ScopedGlobalRef(ScopedGlobalRef&& s) noexcept : mGlobalRef(s.release()) {}
7676

7777
/**
7878
* Move assignment is allowed.
7979
*/
80-
ScopedGlobalRef& operator=(ScopedGlobalRef&& s) {
80+
ScopedGlobalRef& operator=(ScopedGlobalRef&& s) noexcept {
8181
reset(s.release());
8282
return *this;
8383
}

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/ScopedLocalRef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ class ScopedLocalRef {
7070
/**
7171
* Move construction is allowed.
7272
*/
73-
ScopedLocalRef(ScopedLocalRef&& s) : mEnv(s.mEnv), mLocalRef(s.release()) {}
73+
ScopedLocalRef(ScopedLocalRef&& s) noexcept
74+
: mEnv(s.mEnv), mLocalRef(s.release()) {}
7475

7576
/**
7677
* Move assignment is allowed.
7778
*/
78-
ScopedLocalRef& operator=(ScopedLocalRef&& s) {
79+
ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept {
7980
reset(s.release());
8081
mEnv = s.mEnv;
8182
return *this;

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class YGNodeEdges {
3636
BORDER = 4,
3737
};
3838

39-
YGNodeEdges(YGNodeRef node) {
39+
explicit YGNodeEdges(YGNodeRef node) {
4040
auto context = YGNodeContext{};
4141
context.asVoidPtr = YGNodeGetContext(node);
4242
edges_ = context.edgesSet;

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ static void jni_YGConfigSetExperimentalFeatureEnabledJNI(
5454
jboolean enabled) {
5555
const YGConfigRef config = _jlong2YGConfigRef(nativePointer);
5656
YGConfigSetExperimentalFeatureEnabled(
57-
config, static_cast<YGExperimentalFeature>(feature), enabled);
57+
config,
58+
static_cast<YGExperimentalFeature>(feature),
59+
static_cast<bool>(enabled));
5860
}
5961

6062
static void jni_YGConfigSetUseWebDefaultsJNI(
@@ -63,7 +65,7 @@ static void jni_YGConfigSetUseWebDefaultsJNI(
6365
jlong nativePointer,
6466
jboolean useWebDefaults) {
6567
const YGConfigRef config = _jlong2YGConfigRef(nativePointer);
66-
YGConfigSetUseWebDefaults(config, useWebDefaults);
68+
YGConfigSetUseWebDefaults(config, static_cast<bool>(useWebDefaults));
6769
}
6870

6971
static void jni_YGConfigSetPointScaleFactorJNI(
@@ -161,7 +163,7 @@ static void jni_YGConfigSetLoggerJNI(
161163
auto context =
162164
reinterpret_cast<ScopedGlobalRef<jobject>*>(YGConfigGetContext(config));
163165

164-
if (logger) {
166+
if (logger != nullptr) {
165167
if (context == nullptr) {
166168
context = new ScopedGlobalRef<jobject>();
167169
YGConfigSetContext(config, context);
@@ -225,14 +227,15 @@ static void jni_YGNodeSetIsReferenceBaselineJNI(
225227
jlong nativePointer,
226228
jboolean isReferenceBaseline) {
227229
YGNodeSetIsReferenceBaseline(
228-
_jlong2YGNodeRef(nativePointer), isReferenceBaseline);
230+
_jlong2YGNodeRef(nativePointer), static_cast<bool>(isReferenceBaseline));
229231
}
230232

231233
static jboolean jni_YGNodeIsReferenceBaselineJNI(
232234
JNIEnv* /*env*/,
233235
jobject /*obj*/,
234236
jlong nativePointer) {
235-
return YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer));
237+
return static_cast<jboolean>(
238+
YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer)));
236239
}
237240

238241
static void jni_YGNodeRemoveAllChildrenJNI(
@@ -340,7 +343,7 @@ static void jni_YGNodeCalculateLayoutJNI(
340343
try {
341344
PtrJNodeMapVanilla* layoutContext = nullptr;
342345
auto map = PtrJNodeMapVanilla{};
343-
if (nativePointers) {
346+
if (nativePointers != nullptr) {
344347
map = PtrJNodeMapVanilla{nativePointers, javaNodes};
345348
layoutContext = &map;
346349
}
@@ -356,7 +359,7 @@ static void jni_YGNodeCalculateLayoutJNI(
356359
YGTransferLayoutOutputsRecursive(env, obj, root);
357360
} catch (const YogaJniException& jniException) {
358361
ScopedLocalRef<jthrowable> throwable = jniException.getThrowable();
359-
if (throwable.get()) {
362+
if (throwable.get() != nullptr) {
360363
env->Throw(throwable.get());
361364
}
362365
} catch (const std::logic_error& ex) {
@@ -626,8 +629,8 @@ static YGSize YGJNIMeasureFunc(
626629

627630
uint32_t wBits = 0xFFFFFFFF & (measureResult >> 32);
628631
uint32_t hBits = 0xFFFFFFFF & measureResult;
629-
float measuredWidth = std::bit_cast<float>(wBits);
630-
float measuredHeight = std::bit_cast<float>(hBits);
632+
auto measuredWidth = std::bit_cast<float>(wBits);
633+
auto measuredHeight = std::bit_cast<float>(hBits);
631634

632635
return YGSize{measuredWidth, measuredHeight};
633636
} else {
@@ -645,7 +648,7 @@ static void jni_YGNodeSetHasMeasureFuncJNI(
645648
jboolean hasMeasureFunc) {
646649
YGNodeSetMeasureFunc(
647650
_jlong2YGNodeRef(nativePointer),
648-
hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
651+
static_cast<bool>(hasMeasureFunc) ? YGJNIMeasureFunc : nullptr);
649652
}
650653

651654
static float YGJNIBaselineFunc(YGNodeConstRef node, float width, float height) {
@@ -669,7 +672,7 @@ static void jni_YGNodeSetHasBaselineFuncJNI(
669672
jboolean hasBaselineFunc) {
670673
YGNodeSetBaselineFunc(
671674
_jlong2YGNodeRef(nativePointer),
672-
hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
675+
static_cast<bool>(hasBaselineFunc) ? YGJNIBaselineFunc : nullptr);
673676
}
674677

675678
static void jni_YGNodeSetAlwaysFormsContainingBlockJNI(
@@ -678,7 +681,8 @@ static void jni_YGNodeSetAlwaysFormsContainingBlockJNI(
678681
jlong nativePointer,
679682
jboolean alwaysFormsContainingBlock) {
680683
YGNodeSetAlwaysFormsContainingBlock(
681-
_jlong2YGNodeRef(nativePointer), alwaysFormsContainingBlock);
684+
_jlong2YGNodeRef(nativePointer),
685+
static_cast<bool>(alwaysFormsContainingBlock));
682686
}
683687

684688
static jlong

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ YogaJniException::YogaJniException(jthrowable throwable) {
2525
throwable_ = newGlobalRef(getCurrentEnv(), throwable);
2626
}
2727

28-
YogaJniException::YogaJniException(YogaJniException&& rhs)
28+
YogaJniException::YogaJniException(YogaJniException&& rhs) noexcept
2929
: throwable_(std::move(rhs.throwable_)) {}
3030

3131
YogaJniException::YogaJniException(const YogaJniException& rhs) {

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YogaJniException.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class YogaJniException : public std::exception {
2222

2323
explicit YogaJniException(jthrowable throwable);
2424

25-
YogaJniException(YogaJniException&& rhs);
25+
YogaJniException(YogaJniException&& rhs) noexcept;
2626

27-
YogaJniException(const YogaJniException& other);
27+
YogaJniException(const YogaJniException& rhs);
2828

2929
ScopedLocalRef<jthrowable> getThrowable() const noexcept;
3030

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/common.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void registerNatives(
1616
size_t numMethods) {
1717
jclass clazz = env->FindClass(className);
1818

19-
assertNoPendingJniExceptionIf(env, !clazz);
19+
assertNoPendingJniExceptionIf(env, clazz == nullptr);
2020

2121
auto result =
2222
env->RegisterNatives(clazz, methods, static_cast<int32_t>(numMethods));
@@ -31,7 +31,7 @@ jmethodID getStaticMethodId(
3131
const char* methodDescriptor) {
3232
jmethodID methodId =
3333
env->GetStaticMethodID(clazz, methodName, methodDescriptor);
34-
assertNoPendingJniExceptionIf(env, !methodId);
34+
assertNoPendingJniExceptionIf(env, methodId == nullptr);
3535
return methodId;
3636
}
3737

@@ -41,7 +41,7 @@ jmethodID getMethodId(
4141
const char* methodName,
4242
const char* methodDescriptor) {
4343
jmethodID methodId = env->GetMethodID(clazz, methodName, methodDescriptor);
44-
assertNoPendingJniExceptionIf(env, !methodId);
44+
assertNoPendingJniExceptionIf(env, methodId == nullptr);
4545
return methodId;
4646
}
4747

@@ -51,7 +51,7 @@ jfieldID getFieldId(
5151
const char* fieldName,
5252
const char* fieldSignature) {
5353
jfieldID fieldId = env->GetFieldID(clazz, fieldName, fieldSignature);
54-
assertNoPendingJniExceptionIf(env, !fieldId);
54+
assertNoPendingJniExceptionIf(env, fieldId == nullptr);
5555
return fieldId;
5656
}
5757

@@ -82,24 +82,24 @@ callStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID methodId, ...) {
8282
va_start(args, methodId);
8383
jobject result = env->CallStaticObjectMethodV(clazz, methodId, args);
8484
va_end(args);
85-
assertNoPendingJniExceptionIf(env, !result);
85+
assertNoPendingJniExceptionIf(env, result == nullptr);
8686
return make_local_ref(env, result);
8787
}
8888

8989
ScopedGlobalRef<jobject> newGlobalRef(JNIEnv* env, jobject obj) {
9090
jobject result = env->NewGlobalRef(obj);
9191

92-
if (!result) {
92+
if (result == nullptr) {
9393
logErrorMessageAndDie("Could not obtain global reference from object");
9494
}
9595

9696
return make_global_ref(result);
9797
}
9898

9999
ScopedGlobalRef<jthrowable> newGlobalRef(JNIEnv* env, jthrowable obj) {
100-
jthrowable result = static_cast<jthrowable>(env->NewGlobalRef(obj));
100+
auto result = static_cast<jthrowable>(env->NewGlobalRef(obj));
101101

102-
if (!result) {
102+
if (result == nullptr) {
103103
logErrorMessageAndDie("Could not obtain global reference from object");
104104
}
105105

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace facebook::yoga::vanillajni {
1313

1414
namespace {
15-
JavaVM* globalVm = NULL;
15+
JavaVM* globalVm = nullptr;
1616
struct JavaVMInitializer {
17-
JavaVMInitializer(JavaVM* vm) {
17+
explicit JavaVMInitializer(JavaVM* vm) {
1818
if (!vm) {
1919
logErrorMessageAndDie(
2020
"You cannot pass a NULL JavaVM to ensureInitialized");
@@ -27,7 +27,7 @@ struct JavaVMInitializer {
2727
jint ensureInitialized(JNIEnv** env, JavaVM* vm) {
2828
static JavaVMInitializer init(vm);
2929

30-
if (!env) {
30+
if (env == nullptr) {
3131
logErrorMessageAndDie(
3232
"Need to pass a valid JNIEnv pointer to vanillajni initialization "
3333
"routine");
@@ -43,7 +43,7 @@ jint ensureInitialized(JNIEnv** env, JavaVM* vm) {
4343

4444
// TODO why we need JNIEXPORT for getCurrentEnv ?
4545
JNIEXPORT JNIEnv* getCurrentEnv() {
46-
JNIEnv* env;
46+
JNIEnv* env = nullptr;
4747
jint ret = globalVm->GetEnv((void**)&env, JNI_VERSION_1_6);
4848
if (ret != JNI_OK) {
4949
logErrorMessageAndDie(
@@ -68,7 +68,7 @@ void assertNoPendingJniException(JNIEnv* env) {
6868
}
6969

7070
auto throwable = env->ExceptionOccurred();
71-
if (!throwable) {
71+
if (throwable == nullptr) {
7272
logErrorMessageAndDie("Unable to get pending JNI exception.");
7373
}
7474
env->ExceptionClear();

packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/yogajni.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
using namespace facebook::yoga;
1212

13-
jint JNI_OnLoad(JavaVM* vm, void*) {
14-
JNIEnv* env;
13+
jint JNI_OnLoad(JavaVM* vm, void* /*unused*/) {
14+
JNIEnv* env = nullptr;
1515
jint ret = vanillajni::ensureInitialized(&env, vm);
1616
YGJNIVanilla::registerNatives(env);
1717
return ret;

packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ void YGNodeSetChildren(
216216
auto owner = resolveRef(ownerRef);
217217
auto children = reinterpret_cast<yoga::Node* const*>(childrenRefs);
218218

219-
if (!owner) {
219+
if (owner == nullptr) {
220220
return;
221221
}
222222

223223
const std::vector<yoga::Node*> childrenVector = {children, children + count};
224-
if (childrenVector.size() == 0) {
224+
if (childrenVector.empty()) {
225225
if (owner->getChildCount() > 0) {
226226
for (auto* child : owner->getChildren()) {
227227
child->setLayout({});

packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
namespace facebook::yoga {
1414

1515
void layoutAbsoluteChild(
16-
const yoga::Node* const containingNode,
17-
const yoga::Node* const node,
18-
yoga::Node* const child,
19-
const float containingBlockWidth,
20-
const float containingBlockHeight,
21-
const SizingMode widthMode,
22-
const Direction direction,
16+
const yoga::Node* containingNode,
17+
const yoga::Node* node,
18+
yoga::Node* child,
19+
float containingBlockWidth,
20+
float containingBlockHeight,
21+
SizingMode widthMode,
22+
Direction direction,
2323
LayoutData& layoutMarkerData,
24-
const uint32_t depth,
25-
const uint32_t generationCount);
24+
uint32_t depth,
25+
uint32_t generationCount);
2626

2727
void layoutAbsoluteDescendants(
2828
yoga::Node* containingNode,

0 commit comments

Comments
 (0)