diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index a12af246174..283dc972391 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1022,6 +1022,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a mSettings.cppHeaderProbe = false; } + else if (std::strcmp(argv[i], "--no-safety") == 0) + mSettings.safety = false; + // Write results in file else if (std::strncmp(argv[i], "--output-file=", 14) == 0) mSettings.outputFile = Path::simplifyPath(argv[i] + 14); diff --git a/lib/settings.cpp b/lib/settings.cpp index 50fa4b7486e..d7b357ba952 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -162,7 +162,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress const auto& v = it->second; if (!v.is()) return "'safety' is not a bool"; - settings.safety = settings.safety || v.get(); + settings.safety = v.get(); } } diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 195efc31f00..df86b94bf84 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -2026,6 +2026,27 @@ def test_config_invalid(tmpdir): ] +def test_config_override(tmpdir): + # cppcheck.cfg needs to be next to executable + exe = shutil.copy2(__lookup_cppcheck_exe(), tmpdir) + shutil.copytree(os.path.join(os.path.dirname(__lookup_cppcheck_exe()), 'cfg'), os.path.join(tmpdir, 'cfg')) + + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + config_file = os.path.join(tmpdir, 'cppcheck.cfg') + with open(config_file, 'wt') as f: + f.write(json.dumps({ + 'safety': False + })) + + exitcode, stdout, stderr, exe = cppcheck_ex(['-q', '--safety', test_file], cwd=tmpdir, cppcheck_exe=exe, remove_checkers_report=False) + assert exitcode == 0, stdout if stdout else stderr + assert stdout.splitlines() == [] + assert stderr.splitlines() == [] + + def test_checkers_report(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt') as f: diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 64d54f0b2be..5118899b5a0 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -440,6 +440,10 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(debugClangOutput); TEST_CASE(debugXmlMultiple); TEST_CASE(debugNormalXmlMultiple); + TEST_CASE(safety); + TEST_CASE(safetyOverride); + TEST_CASE(noSafety); + TEST_CASE(noSafetyOverride); TEST_CASE(ignorepaths1); TEST_CASE(ignorepaths2); @@ -2974,6 +2978,34 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS("cppcheck: error: printing debug output in XML format does not support multiple input files.\n", logger->str()); } + void safety() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--safety", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); + ASSERT_EQUALS(true, settings->safety); + } + + void safetyOverride() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--no-safety", "--safety", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); + ASSERT_EQUALS(true, settings->safety); + } + + void noSafety() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--no-safety", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); + ASSERT_EQUALS(false, settings->safety); + } + + void noSafetyOverride() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--safety", "--no-safety", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv)); + ASSERT_EQUALS(false, settings->safety); + } + void ignorepaths1() { REDIRECT; const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};