0

I am mutating my code and it's complaining that the following mutation survived:

--- /home/user/src/file.cc 2019-12-03 13:48:12.510311
+++ /home/user/src/file.cc 2019-12-03 14:00:14.164006
@@ -283,7 +283,7 @@
 }

 bool SomeClass::isValid(proto::SomeProto const& task_pb) const {
-    if (task_pb.task_id() == "") {
+    if (task_pb.task_id() < "") {
         log(LOG_WARNING, "no id given");
         return false;
     } else if (task_pb.task_type() == proto::TaskType::UNKNOWN) {

The declaration for task_id() is:

const ::std::string& task_id() const;

Is there a string that can be less than ""? Would this even be considered a valid mutation for strings?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephen Rasku
  • 2,554
  • 7
  • 29
  • 51
  • If `task_id` returns a `const char *`, this is technically valid, but pretty much nonsensical, C++. If `task_id` returns a `std::string`, one of the `<` overloads compares to a `std::string`, and `std::string` can be implicitly constructed from a string literal, so that's what gets compiled. – Sam Varshavchik Dec 04 '19 at 16:22

1 Answers1

0

std::string defines a lexicographic "less-than" relation, that can be accessed using operator<.

The specific expression you've shown will always evaluate to false, because no string sorts (lexicographically) less-than the empty string. But that doesn't make the code invalid, merely pointless. I don't know what you mean by a "valid mutation" exactly, but hopefully that will point you to the answer.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055