Add nodiscard to core math classes to catch c++ errors.

A common source of errors is to call functions (such as round()) expecting them to work in place, but them actually being designed only to return the processed value. Not using the return value in this case in indicative of a bug, and can be flagged as a warning by using the [[nodiscard]] attribute.
This commit is contained in:
lawnjelly
2022-01-20 12:51:13 +00:00
parent 4075866117
commit adf14bfdde
18 changed files with 67 additions and 26 deletions

View File

@@ -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;

View File

@@ -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),

View File

@@ -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,

View File

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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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])

View File

@@ -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,

View File

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