From 0b993d74362535843e8235d2cdc65b775863d1a9 Mon Sep 17 00:00:00 2001 From: Tiago Macarios Date: Tue, 24 Oct 2017 20:58:23 -0700 Subject: [PATCH] Fix catch exception by reference When compiling our codebase with clang we were getting the following error: GSL/tests/algorithm_tests.cpp:198:58: warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference] CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast); ^ Looking at the catch source code exceptions were being caught by value. One could have it designed so that users might say: CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast&); But I don't think this is the intent and looking at the Catch tests itself looks like this macro does not expect the reference: REQUIRE_THROWS_AS( thisThrows(), std::domain_error ); CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error ); This commit makes Catch catch exceptions by reference instead of by value. --- include/internal/catch_capture.hpp | 2 +- projects/SelfTest/CompilationTests.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index 92229df7..aa17b561 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -136,7 +136,7 @@ static_cast(expr); \ __catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \ } \ - catch( exceptionType ) { \ + catch( exceptionType& ) { \ __catchResult.captureResult( Catch::ResultWas::Ok ); \ } \ catch( ... ) { \ diff --git a/projects/SelfTest/CompilationTests.cpp b/projects/SelfTest/CompilationTests.cpp index ed1978f3..30e08ba4 100644 --- a/projects/SelfTest/CompilationTests.cpp +++ b/projects/SelfTest/CompilationTests.cpp @@ -41,7 +41,7 @@ bool templated_tests(T t) { REQUIRE(a == t); CHECK(a == t); REQUIRE_THROWS(throws_int(true)); - CHECK_THROWS_AS(throws_int(true), const int&); + CHECK_THROWS_AS(throws_int(true), const int); REQUIRE_NOTHROW(throws_int(false)); REQUIRE_THAT("aaa", Catch::EndsWith("aaa")); return true;