Reviewed By: jvillard Differential Revision: D8864560 fbshipit-source-id: d0e8c4e91master
parent
36b581cecd
commit
b215cdbff4
@ -0,0 +1,18 @@
|
||||
# Copyright (c) 2016-present, Facebook, Inc.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
TESTS_DIR = ../../..
|
||||
|
||||
ANALYZER = linters
|
||||
CLANG_OPTIONS = -std=c++11 -c
|
||||
INFER_OPTIONS = -a linters --linters-def-file extracopy.al --no-filtering --debug-exceptions --project-root $(TESTS_DIR) \
|
||||
--enable-issue-type GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL
|
||||
INFERPRINT_OPTIONS = --issues-tests
|
||||
|
||||
SOURCES = \
|
||||
$(wildcard *.cpp) \
|
||||
|
||||
|
||||
include $(TESTS_DIR)/clang.make
|
@ -0,0 +1,26 @@
|
||||
// Copyright (c) 2018-present, Facebook, Inc.
|
||||
//
|
||||
// This source code is licensed under the MIT license found in the
|
||||
// LICENSE file in the root directory of this source tree.
|
||||
DEFINE-CHECKER EXTRA_COPY = {
|
||||
LET name_not_contains_copy = NOT declaration_has_name(REGEXP("[cC]opy"));
|
||||
|
||||
LET is_local_var = NOT is_global_var AND NOT is_static_local_var;
|
||||
|
||||
LET is_copy_contructor =
|
||||
HOLDS-NEXT WITH-TRANSITION InitExpr
|
||||
(is_node("CXXConstructExpr") AND is_cxx_copy_constructor);
|
||||
|
||||
SET report_when =
|
||||
WHEN name_not_contains_copy AND is_local_var AND is_copy_contructor
|
||||
HOLDS-IN-NODE VarDecl;
|
||||
|
||||
SET message = "Potentially unnecessary to copy var %name%";
|
||||
SET severity = "WARNING";
|
||||
SET mode = "ON";
|
||||
//SET whitelist_path = {
|
||||
// REGEXP("admarket/.*"),
|
||||
// REGEXP("multifeed/.*")
|
||||
//};
|
||||
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
struct A {
|
||||
int a;
|
||||
};
|
||||
|
||||
const A& get_a_ref() {
|
||||
static A a;
|
||||
return a;
|
||||
}
|
||||
|
||||
A get_a() {
|
||||
A a;
|
||||
return a;
|
||||
}
|
||||
|
||||
int test_a() {
|
||||
static auto x = get_a_ref();
|
||||
auto y = get_a_ref();
|
||||
auto copy_y = get_a_ref();
|
||||
auto z = get_a();
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto global_a = get_a_ref();
|
||||
|
||||
void test_map() {
|
||||
std::map<int, int> intMap{{1, 2}, {3, 4}};
|
||||
for (auto p : intMap) {
|
||||
std::cout << p.first << "->" << p.second << std::endl;
|
||||
}
|
||||
for (auto copy_p : intMap) {
|
||||
std::cout << copy_p.first << "->" << copy_p.second << std::endl;
|
||||
}
|
||||
for (const auto& p : intMap) {
|
||||
std::cout << p.first << "->" << p.second << std::endl;
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
codetoanalyze/cpp/linters/extracopy.cpp, test_a, 33, EXTRA_COPY, no_bucket, WARNING, []
|
||||
codetoanalyze/cpp/linters/extracopy.cpp, test_map, 43, EXTRA_COPY, no_bucket, WARNING, []
|
Loading…
Reference in new issue