Skip to content

Commit e0ad732

Browse files
authored
Merge pull request #3105 from frewsxcv/frewsxcv-private
Make clippy_lints::{utils,consts} modules private, remove unused items.
2 parents 0f2eab6 + d5534ca commit e0ad732

File tree

11 files changed

+202
-253
lines changed

11 files changed

+202
-253
lines changed

ci/base-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ remark -f *.md > /dev/null
77
# build clippy in debug mode and run tests
88
cargo build --features debugging
99
cargo test --features debugging
10+
cd clippy_lints && cargo test && cd ..
1011
mkdir -p ~/rust/cargo/bin
1112
cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
1213
cp target/debug/clippy-driver ~/rust/cargo/bin/clippy-driver

clippy_lints/src/consts.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,6 @@ use syntax::ast::{FloatTy, LitKind};
1616
use syntax::ptr::P;
1717
use crate::utils::{sext, unsext, clip};
1818

19-
#[derive(Debug, Copy, Clone)]
20-
pub enum FloatWidth {
21-
F32,
22-
F64,
23-
Any,
24-
}
25-
26-
impl From<FloatTy> for FloatWidth {
27-
fn from(ty: FloatTy) -> Self {
28-
match ty {
29-
FloatTy::F32 => FloatWidth::F32,
30-
FloatTy::F64 => FloatWidth::F64,
31-
}
32-
}
33-
}
34-
3519
/// A `LitKind`-like enum to fold constant `Expr`s into.
3620
#[derive(Debug, Clone)]
3721
pub enum Constant {

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ macro_rules! declare_clippy_lint {
4848
};
4949
}
5050

51-
pub mod consts;
51+
mod consts;
5252
#[macro_use]
53-
pub mod utils;
53+
mod utils;
5454

5555
// begin lints modules, do not remove this comment, it’s used in `update_lints`
5656
pub mod approx_const;

clippy_lints/src/utils/camel_case.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/// Return the index of the character after the first camel-case component of
2+
/// `s`.
3+
pub fn camel_case_until(s: &str) -> usize {
4+
let mut iter = s.char_indices();
5+
if let Some((_, first)) = iter.next() {
6+
if !first.is_uppercase() {
7+
return 0;
8+
}
9+
} else {
10+
return 0;
11+
}
12+
let mut up = true;
13+
let mut last_i = 0;
14+
for (i, c) in iter {
15+
if up {
16+
if c.is_lowercase() {
17+
up = false;
18+
} else {
19+
return last_i;
20+
}
21+
} else if c.is_uppercase() {
22+
up = true;
23+
last_i = i;
24+
} else if !c.is_lowercase() {
25+
return i;
26+
}
27+
}
28+
if up {
29+
last_i
30+
} else {
31+
s.len()
32+
}
33+
}
34+
35+
/// Return index of the last camel-case component of `s`.
36+
pub fn camel_case_from(s: &str) -> usize {
37+
let mut iter = s.char_indices().rev();
38+
if let Some((_, first)) = iter.next() {
39+
if !first.is_lowercase() {
40+
return s.len();
41+
}
42+
} else {
43+
return s.len();
44+
}
45+
let mut down = true;
46+
let mut last_i = s.len();
47+
for (i, c) in iter {
48+
if down {
49+
if c.is_uppercase() {
50+
down = false;
51+
last_i = i;
52+
} else if !c.is_lowercase() {
53+
return last_i;
54+
}
55+
} else if c.is_lowercase() {
56+
down = true;
57+
} else {
58+
return last_i;
59+
}
60+
}
61+
last_i
62+
}
63+
64+
#[cfg(test)]
65+
mod test {
66+
use super::{camel_case_from, camel_case_until};
67+
68+
#[test]
69+
fn from_full() {
70+
assert_eq!(camel_case_from("AbcDef"), 0);
71+
assert_eq!(camel_case_from("Abc"), 0);
72+
}
73+
74+
#[test]
75+
fn from_partial() {
76+
assert_eq!(camel_case_from("abcDef"), 3);
77+
assert_eq!(camel_case_from("aDbc"), 1);
78+
}
79+
80+
#[test]
81+
fn from_not() {
82+
assert_eq!(camel_case_from("AbcDef_"), 7);
83+
assert_eq!(camel_case_from("AbcDD"), 5);
84+
}
85+
86+
#[test]
87+
fn from_caps() {
88+
assert_eq!(camel_case_from("ABCD"), 4);
89+
}
90+
91+
#[test]
92+
fn until_full() {
93+
assert_eq!(camel_case_until("AbcDef"), 6);
94+
assert_eq!(camel_case_until("Abc"), 3);
95+
}
96+
97+
#[test]
98+
fn until_not() {
99+
assert_eq!(camel_case_until("abcDef"), 0);
100+
assert_eq!(camel_case_until("aDbc"), 0);
101+
}
102+
103+
#[test]
104+
fn until_partial() {
105+
assert_eq!(camel_case_until("AbcDef_"), 6);
106+
assert_eq!(camel_case_until("CallTypeC"), 8);
107+
assert_eq!(camel_case_until("AbcDD"), 3);
108+
}
109+
110+
#[test]
111+
fn until_caps() {
112+
assert_eq!(camel_case_until("ABCD"), 0);
113+
}
114+
}

clippy_lints/src/utils/conf.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,13 @@ pub enum Error {
3838
Io(io::Error),
3939
/// Not valid toml or doesn't fit the expected conf format
4040
Toml(String),
41-
/// Type error.
42-
Type(
43-
/// The name of the key.
44-
&'static str,
45-
/// The expected type.
46-
&'static str,
47-
/// The type we got instead.
48-
&'static str,
49-
),
50-
/// There is an unknown key is the file.
51-
UnknownKey(String),
5241
}
5342

5443
impl fmt::Display for Error {
5544
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
5645
match *self {
5746
Error::Io(ref err) => err.fmt(f),
5847
Error::Toml(ref err) => err.fmt(f),
59-
Error::Type(key, expected, got) => {
60-
write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
61-
},
62-
Error::UnknownKey(ref key) => write!(f, "unknown key `{}`", key),
6348
}
6449
}
6550
}

clippy_lints/src/utils/mod.rs

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ use syntax::ast::{self, LitKind};
2121
use syntax::attr;
2222
use syntax::source_map::{Span, DUMMY_SP};
2323
use syntax::errors::DiagnosticBuilder;
24-
use syntax::ptr::P;
2524
use syntax::symbol::keywords;
2625

26+
mod camel_case;
27+
pub use self::camel_case::{camel_case_from, camel_case_until};
28+
2729
pub mod comparisons;
2830
pub mod conf;
2931
pub mod constants;
@@ -37,8 +39,6 @@ pub mod ptr;
3739
pub mod usage;
3840
pub use self::hir_utils::{SpanlessEq, SpanlessHash};
3941

40-
pub type MethodArgs = HirVec<P<Expr>>;
41-
4242
pub mod higher;
4343

4444
/// Returns true if the two spans come from differing expansions (i.e. one is
@@ -106,17 +106,6 @@ pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
106106
}
107107
}
108108

109-
/// Check if the method call given in `expr` belongs to given type.
110-
pub fn match_impl_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
111-
let method_call = cx.tables.type_dependent_defs()[expr.hir_id];
112-
let trt_id = cx.tcx.impl_of_method(method_call.def_id());
113-
if let Some(trt_id) = trt_id {
114-
match_def_path(cx.tcx, trt_id, path)
115-
} else {
116-
false
117-
}
118-
}
119-
120109
/// Check if the method call given in `expr` belongs to given trait.
121110
pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
122111
let method_call = cx.tables.type_dependent_defs()[expr.hir_id];
@@ -755,69 +744,6 @@ pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
755744
}
756745
}
757746

758-
/// Return the index of the character after the first camel-case component of
759-
/// `s`.
760-
pub fn camel_case_until(s: &str) -> usize {
761-
let mut iter = s.char_indices();
762-
if let Some((_, first)) = iter.next() {
763-
if !first.is_uppercase() {
764-
return 0;
765-
}
766-
} else {
767-
return 0;
768-
}
769-
let mut up = true;
770-
let mut last_i = 0;
771-
for (i, c) in iter {
772-
if up {
773-
if c.is_lowercase() {
774-
up = false;
775-
} else {
776-
return last_i;
777-
}
778-
} else if c.is_uppercase() {
779-
up = true;
780-
last_i = i;
781-
} else if !c.is_lowercase() {
782-
return i;
783-
}
784-
}
785-
if up {
786-
last_i
787-
} else {
788-
s.len()
789-
}
790-
}
791-
792-
/// Return index of the last camel-case component of `s`.
793-
pub fn camel_case_from(s: &str) -> usize {
794-
let mut iter = s.char_indices().rev();
795-
if let Some((_, first)) = iter.next() {
796-
if !first.is_lowercase() {
797-
return s.len();
798-
}
799-
} else {
800-
return s.len();
801-
}
802-
let mut down = true;
803-
let mut last_i = s.len();
804-
for (i, c) in iter {
805-
if down {
806-
if c.is_uppercase() {
807-
down = false;
808-
last_i = i;
809-
} else if !c.is_lowercase() {
810-
return last_i;
811-
}
812-
} else if c.is_lowercase() {
813-
down = true;
814-
} else {
815-
return last_i;
816-
}
817-
}
818-
last_i
819-
}
820-
821747
/// Convenience function to get the return type of a function
822748
pub fn return_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, fn_item: NodeId) -> Ty<'tcx> {
823749
let fn_def_id = cx.tcx.hir.local_def_id(fn_item);
@@ -1109,3 +1035,84 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_, '_, '_>, node: NodeId
11091035
}
11101036
false
11111037
}
1038+
1039+
#[cfg(test)]
1040+
mod test {
1041+
use super::{trim_multiline, without_block_comments};
1042+
1043+
#[test]
1044+
fn test_trim_multiline_single_line() {
1045+
assert_eq!("", trim_multiline("".into(), false));
1046+
assert_eq!("...", trim_multiline("...".into(), false));
1047+
assert_eq!("...", trim_multiline(" ...".into(), false));
1048+
assert_eq!("...", trim_multiline("\t...".into(), false));
1049+
assert_eq!("...", trim_multiline("\t\t...".into(), false));
1050+
}
1051+
1052+
#[test]
1053+
#[rustfmt::skip]
1054+
fn test_trim_multiline_block() {
1055+
assert_eq!("\
1056+
if x {
1057+
y
1058+
} else {
1059+
z
1060+
}", trim_multiline(" if x {
1061+
y
1062+
} else {
1063+
z
1064+
}".into(), false));
1065+
assert_eq!("\
1066+
if x {
1067+
\ty
1068+
} else {
1069+
\tz
1070+
}", trim_multiline(" if x {
1071+
\ty
1072+
} else {
1073+
\tz
1074+
}".into(), false));
1075+
}
1076+
1077+
#[test]
1078+
#[rustfmt::skip]
1079+
fn test_trim_multiline_empty_line() {
1080+
assert_eq!("\
1081+
if x {
1082+
y
1083+
1084+
} else {
1085+
z
1086+
}", trim_multiline(" if x {
1087+
y
1088+
1089+
} else {
1090+
z
1091+
}".into(), false));
1092+
}
1093+
1094+
#[test]
1095+
fn test_without_block_comments_lines_without_block_comments() {
1096+
let result = without_block_comments(vec!["/*", "", "*/"]);
1097+
println!("result: {:?}", result);
1098+
assert!(result.is_empty());
1099+
1100+
let result = without_block_comments(vec!["", "/*", "", "*/", "#[crate_type = \"lib\"]", "/*", "", "*/", ""]);
1101+
assert_eq!(result, vec!["", "#[crate_type = \"lib\"]", ""]);
1102+
1103+
let result = without_block_comments(vec!["/* rust", "", "*/"]);
1104+
assert!(result.is_empty());
1105+
1106+
let result = without_block_comments(vec!["/* one-line comment */"]);
1107+
assert!(result.is_empty());
1108+
1109+
let result = without_block_comments(vec!["/* nested", "/* multi-line", "comment", "*/", "test", "*/"]);
1110+
assert!(result.is_empty());
1111+
1112+
let result = without_block_comments(vec!["/* nested /* inline /* comment */ test */ */"]);
1113+
assert!(result.is_empty());
1114+
1115+
let result = without_block_comments(vec!["foo", "bar", "baz"]);
1116+
assert_eq!(result, vec!["foo", "bar", "baz"]);
1117+
}
1118+
}

0 commit comments

Comments
 (0)