Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make clippy_lints::{utils,consts} modules private, remove unused items. #3105

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/base-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ remark -f *.md > /dev/null
# build clippy in debug mode and run tests
cargo build --features debugging
cargo test --features debugging
cd clippy_lints && cargo test && cd ..
mkdir -p ~/rust/cargo/bin
cp target/debug/cargo-clippy ~/rust/cargo/bin/cargo-clippy
cp target/debug/clippy-driver ~/rust/cargo/bin/clippy-driver
Expand Down
16 changes: 0 additions & 16 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ use syntax::ast::{FloatTy, LitKind};
use syntax::ptr::P;
use crate::utils::{sext, unsext, clip};

#[derive(Debug, Copy, Clone)]
pub enum FloatWidth {
F32,
F64,
Any,
}

impl From<FloatTy> for FloatWidth {
fn from(ty: FloatTy) -> Self {
match ty {
FloatTy::F32 => FloatWidth::F32,
FloatTy::F64 => FloatWidth::F64,
}
}
}

/// A `LitKind`-like enum to fold constant `Expr`s into.
#[derive(Debug, Clone)]
pub enum Constant {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ macro_rules! declare_clippy_lint {
};
}

pub mod consts;
mod consts;
#[macro_use]
pub mod utils;
mod utils;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RLS use any utils anywhere else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.


// begin lints modules, do not remove this comment, it’s used in `update_lints`
pub mod approx_const;
Expand Down
114 changes: 114 additions & 0 deletions clippy_lints/src/utils/camel_case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/// Return the index of the character after the first camel-case component of
/// `s`.
pub fn camel_case_until(s: &str) -> usize {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved from clippy_lints/src/utils/mod.rs

let mut iter = s.char_indices();
if let Some((_, first)) = iter.next() {
if !first.is_uppercase() {
return 0;
}
} else {
return 0;
}
let mut up = true;
let mut last_i = 0;
for (i, c) in iter {
if up {
if c.is_lowercase() {
up = false;
} else {
return last_i;
}
} else if c.is_uppercase() {
up = true;
last_i = i;
} else if !c.is_lowercase() {
return i;
}
}
if up {
last_i
} else {
s.len()
}
}

/// Return index of the last camel-case component of `s`.
pub fn camel_case_from(s: &str) -> usize {
let mut iter = s.char_indices().rev();
if let Some((_, first)) = iter.next() {
if !first.is_lowercase() {
return s.len();
}
} else {
return s.len();
}
let mut down = true;
let mut last_i = s.len();
for (i, c) in iter {
if down {
if c.is_uppercase() {
down = false;
last_i = i;
} else if !c.is_lowercase() {
return last_i;
}
} else if c.is_lowercase() {
down = true;
} else {
return last_i;
}
}
last_i
}

#[cfg(test)]
mod test {
use super::{camel_case_from, camel_case_until};

#[test]
fn from_full() {
assert_eq!(camel_case_from("AbcDef"), 0);
assert_eq!(camel_case_from("Abc"), 0);
}

#[test]
fn from_partial() {
assert_eq!(camel_case_from("abcDef"), 3);
assert_eq!(camel_case_from("aDbc"), 1);
}

#[test]
fn from_not() {
assert_eq!(camel_case_from("AbcDef_"), 7);
assert_eq!(camel_case_from("AbcDD"), 5);
}

#[test]
fn from_caps() {
assert_eq!(camel_case_from("ABCD"), 4);
}

#[test]
fn until_full() {
assert_eq!(camel_case_until("AbcDef"), 6);
assert_eq!(camel_case_until("Abc"), 3);
}

#[test]
fn until_not() {
assert_eq!(camel_case_until("abcDef"), 0);
assert_eq!(camel_case_until("aDbc"), 0);
}

#[test]
fn until_partial() {
assert_eq!(camel_case_until("AbcDef_"), 6);
assert_eq!(camel_case_until("CallTypeC"), 8);
assert_eq!(camel_case_until("AbcDD"), 3);
}

#[test]
fn until_caps() {
assert_eq!(camel_case_until("ABCD"), 0);
}
}
15 changes: 0 additions & 15 deletions clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,13 @@ pub enum Error {
Io(io::Error),
/// Not valid toml or doesn't fit the expected conf format
Toml(String),
/// Type error.
Type(
/// The name of the key.
&'static str,
/// The expected type.
&'static str,
/// The type we got instead.
&'static str,
),
/// There is an unknown key is the file.
UnknownKey(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match *self {
Error::Io(ref err) => err.fmt(f),
Error::Toml(ref err) => err.fmt(f),
Error::Type(key, expected, got) => {
write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
},
Error::UnknownKey(ref key) => write!(f, "unknown key `{}`", key),
}
}
}
Expand Down
161 changes: 84 additions & 77 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ use syntax::ast::{self, LitKind};
use syntax::attr;
use syntax::source_map::{Span, DUMMY_SP};
use syntax::errors::DiagnosticBuilder;
use syntax::ptr::P;
use syntax::symbol::keywords;

mod camel_case;
pub use self::camel_case::{camel_case_from, camel_case_until};

pub mod comparisons;
pub mod conf;
pub mod constants;
Expand All @@ -37,8 +39,6 @@ pub mod ptr;
pub mod usage;
pub use self::hir_utils::{SpanlessEq, SpanlessHash};

pub type MethodArgs = HirVec<P<Expr>>;

pub mod higher;

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

/// Check if the method call given in `expr` belongs to given type.
pub fn match_impl_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
let method_call = cx.tables.type_dependent_defs()[expr.hir_id];
let trt_id = cx.tcx.impl_of_method(method_call.def_id());
if let Some(trt_id) = trt_id {
match_def_path(cx.tcx, trt_id, path)
} else {
false
}
}

/// Check if the method call given in `expr` belongs to given trait.
pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
let method_call = cx.tables.type_dependent_defs()[expr.hir_id];
Expand Down Expand Up @@ -755,69 +744,6 @@ pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
}
}

/// Return the index of the character after the first camel-case component of
/// `s`.
pub fn camel_case_until(s: &str) -> usize {
let mut iter = s.char_indices();
if let Some((_, first)) = iter.next() {
if !first.is_uppercase() {
return 0;
}
} else {
return 0;
}
let mut up = true;
let mut last_i = 0;
for (i, c) in iter {
if up {
if c.is_lowercase() {
up = false;
} else {
return last_i;
}
} else if c.is_uppercase() {
up = true;
last_i = i;
} else if !c.is_lowercase() {
return i;
}
}
if up {
last_i
} else {
s.len()
}
}

/// Return index of the last camel-case component of `s`.
pub fn camel_case_from(s: &str) -> usize {
let mut iter = s.char_indices().rev();
if let Some((_, first)) = iter.next() {
if !first.is_lowercase() {
return s.len();
}
} else {
return s.len();
}
let mut down = true;
let mut last_i = s.len();
for (i, c) in iter {
if down {
if c.is_uppercase() {
down = false;
last_i = i;
} else if !c.is_lowercase() {
return last_i;
}
} else if c.is_lowercase() {
down = true;
} else {
return last_i;
}
}
last_i
}

/// Convenience function to get the return type of a function
pub fn return_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, fn_item: NodeId) -> Ty<'tcx> {
let fn_def_id = cx.tcx.hir.local_def_id(fn_item);
Expand Down Expand Up @@ -1109,3 +1035,84 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_, '_, '_>, node: NodeId
}
false
}

#[cfg(test)]
mod test {
use super::{trim_multiline, without_block_comments};

#[test]
fn test_trim_multiline_single_line() {
assert_eq!("", trim_multiline("".into(), false));
assert_eq!("...", trim_multiline("...".into(), false));
assert_eq!("...", trim_multiline(" ...".into(), false));
assert_eq!("...", trim_multiline("\t...".into(), false));
assert_eq!("...", trim_multiline("\t\t...".into(), false));
}

#[test]
#[rustfmt::skip]
fn test_trim_multiline_block() {
assert_eq!("\
if x {
y
} else {
z
}", trim_multiline(" if x {
y
} else {
z
}".into(), false));
assert_eq!("\
if x {
\ty
} else {
\tz
}", trim_multiline(" if x {
\ty
} else {
\tz
}".into(), false));
}

#[test]
#[rustfmt::skip]
fn test_trim_multiline_empty_line() {
assert_eq!("\
if x {
y

} else {
z
}", trim_multiline(" if x {
y

} else {
z
}".into(), false));
}

#[test]
fn test_without_block_comments_lines_without_block_comments() {
let result = without_block_comments(vec!["/*", "", "*/"]);
println!("result: {:?}", result);
assert!(result.is_empty());

let result = without_block_comments(vec!["", "/*", "", "*/", "#[crate_type = \"lib\"]", "/*", "", "*/", ""]);
assert_eq!(result, vec!["", "#[crate_type = \"lib\"]", ""]);

let result = without_block_comments(vec!["/* rust", "", "*/"]);
assert!(result.is_empty());

let result = without_block_comments(vec!["/* one-line comment */"]);
assert!(result.is_empty());

let result = without_block_comments(vec!["/* nested", "/* multi-line", "comment", "*/", "test", "*/"]);
assert!(result.is_empty());

let result = without_block_comments(vec!["/* nested /* inline /* comment */ test */ */"]);
assert!(result.is_empty());

let result = without_block_comments(vec!["foo", "bar", "baz"]);
assert_eq!(result, vec!["foo", "bar", "baz"]);
}
}
Loading