Przeglądaj źródła

Merge pull request #56999 from lawnjelly/no_discard

Rémi Verschelde 3 lat temu
rodzic
commit
08cabf2a36

+ 1 - 1
core/color.h

@@ -34,7 +34,7 @@
 #include "core/math/math_funcs.h"
 #include "core/ustring.h"
 
-struct Color {
+struct _NO_DISCARD_CLASS_ Color {
 	union {
 		struct {
 			float r;

+ 1 - 1
core/math/aabb.h

@@ -40,7 +40,7 @@
  * This is implemented by a point (position) and the box size
  */
 
-class AABB {
+class _NO_DISCARD_CLASS_ AABB {
 public:
 	Vector3 position;
 	Vector3 size;

+ 1 - 1
core/math/basis.h

@@ -34,7 +34,7 @@
 #include "core/math/quat.h"
 #include "core/math/vector3.h"
 
-class Basis {
+class _NO_DISCARD_CLASS_ Basis {
 public:
 	Vector3 elements[3] = {
 		Vector3(1, 0, 0),

+ 1 - 1
core/math/face3.h

@@ -36,7 +36,7 @@
 #include "core/math/transform.h"
 #include "core/math/vector3.h"
 
-class Face3 {
+class _NO_DISCARD_CLASS_ Face3 {
 public:
 	enum Side {
 		SIDE_OVER,

+ 1 - 1
core/math/plane.h

@@ -33,7 +33,7 @@
 
 #include "core/math/vector3.h"
 
-class Plane {
+class _NO_DISCARD_CLASS_ Plane {
 public:
 	Vector3 normal;
 	real_t d;

+ 2 - 2
core/math/quat.h

@@ -36,7 +36,7 @@
 #include "core/math/vector3.h"
 #include "core/ustring.h"
 
-class Quat {
+class _NO_DISCARD_CLASS_ Quat {
 public:
 	real_t x, y, z, w;
 
@@ -127,7 +127,7 @@ public:
 			w(p_q.w) {
 	}
 
-	Quat operator=(const Quat &p_q) {
+	Quat &operator=(const Quat &p_q) {
 		x = p_q.x;
 		y = p_q.y;
 		z = p_q.z;

+ 2 - 2
core/math/rect2.h

@@ -35,7 +35,7 @@
 
 struct Transform2D;
 
-struct Rect2 {
+struct _NO_DISCARD_CLASS_ Rect2 {
 	Point2 position;
 	Size2 size;
 
@@ -259,7 +259,7 @@ struct Rect2 {
 	}
 };
 
-struct Rect2i {
+struct _NO_DISCARD_CLASS_ Rect2i {
 	Point2i position;
 	Size2i size;
 

+ 1 - 1
core/math/transform.h

@@ -36,7 +36,7 @@
 #include "core/math/plane.h"
 #include "core/pool_vector.h"
 
-class Transform {
+class _NO_DISCARD_CLASS_ Transform {
 public:
 	Basis basis;
 	Vector3 origin;

+ 1 - 1
core/math/transform_2d.h

@@ -34,7 +34,7 @@
 #include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring
 #include "core/pool_vector.h"
 
-struct Transform2D {
+struct _NO_DISCARD_CLASS_ Transform2D {
 	// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
 	// M = (elements[0][0] elements[1][0])
 	//     (elements[0][1] elements[1][1])

+ 2 - 2
core/math/vector2.h

@@ -36,7 +36,7 @@
 
 struct Vector2i;
 
-struct Vector2 {
+struct _NO_DISCARD_CLASS_ Vector2 {
 	static const int AXIS_COUNT = 2;
 
 	enum Axis {
@@ -269,7 +269,7 @@ typedef Vector2 Point2;
 
 /* INTEGER STUFF */
 
-struct Vector2i {
+struct _NO_DISCARD_CLASS_ Vector2i {
 	enum Axis {
 		AXIS_X,
 		AXIS_Y,

+ 1 - 1
core/math/vector3.h

@@ -36,7 +36,7 @@
 
 class Basis;
 
-struct Vector3 {
+struct _NO_DISCARD_CLASS_ Vector3 {
 	static const int AXIS_COUNT = 3;
 
 	enum Axis {

+ 41 - 0
core/typedefs.h

@@ -69,6 +69,47 @@
 
 #endif
 
+// No discard allows the compiler to flag warnings if we don't use the return value of functions / classes
+#ifndef _NO_DISCARD_
+// c++ 17 onwards
+#if __cplusplus >= 201703L
+#define _NO_DISCARD_ [[nodiscard]]
+#else
+// __warn_unused_result__ supported on clang and GCC
+#if (defined(__clang__) || defined(__GNUC__)) && defined(__has_attribute)
+#if __has_attribute(__warn_unused_result__)
+#define _NO_DISCARD_ __attribute__((__warn_unused_result__))
+#endif
+#endif
+
+// Visual Studio 2012 onwards
+#if _MSC_VER >= 1700
+#define _NO_DISCARD_ _Check_return_
+#endif
+
+// If nothing supported, just noop the macro
+#ifndef _NO_DISCARD_
+#define _NO_DISCARD_
+#endif
+#endif // not c++ 17
+#endif // not defined _NO_DISCARD_
+
+// In some cases _NO_DISCARD_ will get false positives,
+// we can prevent the warning in specific cases by preceding the call with a cast.
+#ifndef _ALLOW_DISCARD_
+#define _ALLOW_DISCARD_ (void)
+#endif
+
+// GCC (prior to c++ 17) Does not seem to support no discard with classes, only functions.
+// So we will use a specific macro for classes.
+#ifndef _NO_DISCARD_CLASS_
+#if (defined(__clang__) || defined(_MSC_VER))
+#define _NO_DISCARD_CLASS_ _NO_DISCARD_
+#else
+#define _NO_DISCARD_CLASS_
+#endif
+#endif
+
 //custom, gcc-safe offsetof, because gcc complains a lot.
 template <class T>
 T *_nullptr() {

+ 2 - 2
modules/bullet/cone_twist_joint_bullet.cpp

@@ -43,14 +43,14 @@
 ConeTwistJointBullet::ConeTwistJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &rbAFrame, const Transform &rbBFrame) :
 		JointBullet() {
 	Transform scaled_AFrame(rbAFrame.scaled(rbA->get_body_scale()));
-	scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
+	_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
 
 	btTransform btFrameA;
 	G_TO_B(scaled_AFrame, btFrameA);
 
 	if (rbB) {
 		Transform scaled_BFrame(rbBFrame.scaled(rbB->get_body_scale()));
-		scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
+		_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
 
 		btTransform btFrameB;
 		G_TO_B(scaled_BFrame, btFrameB);

+ 2 - 2
modules/bullet/generic_6dof_joint_bullet.cpp

@@ -44,7 +44,7 @@ Generic6DOFJointBullet::Generic6DOFJointBullet(RigidBodyBullet *rbA, RigidBodyBu
 		JointBullet() {
 	Transform scaled_AFrame(frameInA.scaled(rbA->get_body_scale()));
 
-	scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
+	_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
 
 	btTransform btFrameA;
 	G_TO_B(scaled_AFrame, btFrameA);
@@ -52,7 +52,7 @@ Generic6DOFJointBullet::Generic6DOFJointBullet(RigidBodyBullet *rbA, RigidBodyBu
 	if (rbB) {
 		Transform scaled_BFrame(frameInB.scaled(rbB->get_body_scale()));
 
-		scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
+		_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
 
 		btTransform btFrameB;
 		G_TO_B(scaled_BFrame, btFrameB);

+ 2 - 2
modules/bullet/hinge_joint_bullet.cpp

@@ -43,14 +43,14 @@
 HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameA, const Transform &frameB) :
 		JointBullet() {
 	Transform scaled_AFrame(frameA.scaled(rbA->get_body_scale()));
-	scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
+	_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
 
 	btTransform btFrameA;
 	G_TO_B(scaled_AFrame, btFrameA);
 
 	if (rbB) {
 		Transform scaled_BFrame(frameB.scaled(rbB->get_body_scale()));
-		scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
+		_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
 
 		btTransform btFrameB;
 		G_TO_B(scaled_BFrame, btFrameB);

+ 2 - 2
modules/bullet/slider_joint_bullet.cpp

@@ -43,14 +43,14 @@
 SliderJointBullet::SliderJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameInA, const Transform &frameInB) :
 		JointBullet() {
 	Transform scaled_AFrame(frameInA.scaled(rbA->get_body_scale()));
-	scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
+	_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
 
 	btTransform btFrameA;
 	G_TO_B(scaled_AFrame, btFrameA);
 
 	if (rbB) {
 		Transform scaled_BFrame(frameInB.scaled(rbB->get_body_scale()));
-		scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
+		_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
 
 		btTransform btFrameB;
 		G_TO_B(scaled_BFrame, btFrameB);

+ 3 - 3
platform/osx/os_osx.mm

@@ -426,7 +426,7 @@ static NSCursor *cursorFromSelector(SEL selector, SEL fallback = nil) {
 
 - (void)windowDidBecomeKey:(NSNotification *)notification {
 	if (OS_OSX::singleton->get_main_loop()) {
-		get_mouse_pos([OS_OSX::singleton->window_object mouseLocationOutsideOfEventStream]);
+		_ALLOW_DISCARD_ get_mouse_pos([OS_OSX::singleton->window_object mouseLocationOutsideOfEventStream]);
 		OS_OSX::singleton->input->set_mouse_position(Point2(mouse_x, mouse_y));
 
 		OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
@@ -1370,7 +1370,7 @@ inline void sendPanEvent(double dx, double dy, int modifierFlags) {
 - (void)scrollWheel:(NSEvent *)event {
 	double deltaX, deltaY;
 
-	get_mouse_pos([event locationInWindow]);
+	_ALLOW_DISCARD_ get_mouse_pos([event locationInWindow]);
 
 	deltaX = [event scrollingDeltaX];
 	deltaY = [event scrollingDeltaY];
@@ -2167,7 +2167,7 @@ void OS_OSX::warp_mouse_position(const Point2 &p_to) {
 }
 
 void OS_OSX::update_real_mouse_position() {
-	get_mouse_pos([window_object mouseLocationOutsideOfEventStream]);
+	_ALLOW_DISCARD_ get_mouse_pos([window_object mouseLocationOutsideOfEventStream]);
 	input->set_mouse_position(Point2(mouse_x, mouse_y));
 }
 

+ 1 - 1
scene/2d/canvas_item.cpp

@@ -1247,7 +1247,7 @@ void CanvasItem::set_notify_transform(bool p_enable) {
 
 	if (notify_transform && is_inside_tree()) {
 		//this ensures that invalid globals get resolved, so notifications can be received
-		get_global_transform();
+		_ALLOW_DISCARD_ get_global_transform();
 	}
 }