Skip to content

Fix some clippy lints #138

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

Merged
merged 11 commits into from
Oct 22, 2018
Merged
8 changes: 7 additions & 1 deletion crates/ra_analysis/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ salsa::query_group! {
}
}

#[derive(Default, Debug, PartialEq, Eq)]
#[derive(Default, Debug, Eq)]
pub(crate) struct FileSet {
pub(crate) files: FxHashSet<FileId>,
pub(crate) resolver: FileResolverImp,
}

impl PartialEq for FileSet {
fn eq(&self, other: &FileSet) -> bool {
self.files == other.files && self.resolver == other.resolver
}
}

impl Hash for FileSet {
fn hash<H: Hasher>(&self, hasher: &mut H) {
let mut files = self.files.iter().cloned().collect::<Vec<_>>();
Expand Down
7 changes: 3 additions & 4 deletions crates/ra_analysis/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ModuleDescriptor {
}
}

fn modules<'a>(root: ast::Root<'a>) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> {
fn modules(root: ast::Root<'_>) -> impl Iterator<Item = (SmolStr, ast::Module<'_>)> {
root.modules().filter_map(|module| {
let name = module.name()?.text();
if !module.has_semi() {
Expand Down Expand Up @@ -183,8 +183,7 @@ impl Link {
root: ast::Root<'a>,
) -> ast::Module<'a> {
modules(root)
.filter(|(name, _)| name == &tree.link(self).name)
.next()
.find(|(name, _)| name == &tree.link(self).name)
.unwrap()
.1
}
Expand Down Expand Up @@ -233,7 +232,7 @@ pub struct FnDescriptor {
}

impl FnDescriptor {
pub fn new(node: ast::FnDef) -> Option<Self> {
pub fn new_opt(node: ast::FnDef) -> Option<Self> {
let name = node.name()?.text().to_string();

// Strip the body out for the label.
Expand Down
6 changes: 3 additions & 3 deletions crates/ra_analysis/src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl AnalysisImpl {
for (_, fs) in file_symbols {
if fs.kind == FN_DEF {
if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) {
if let Some(descriptor) = FnDescriptor::new(fn_def) {
if let Some(descriptor) = FnDescriptor::new_opt(fn_def) {
// If we have a calling expression let's find which argument we are on
let mut current_parameter = None;

Expand All @@ -388,12 +388,12 @@ impl AnalysisImpl {
.text()
.slice(range_search)
.to_string()
.matches(",")
.matches(',')
.count();

// If we have a method call eat the first param since it's just self.
if has_self {
commas = commas + 1;
commas += 1;
}

current_parameter = Some(commas);
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_analysis/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct JobToken {
}

impl JobHandle {
pub fn new() -> (JobHandle, JobToken) {
pub fn new_pair() -> (JobHandle, JobToken) {
Copy link
Member

Choose a reason for hiding this comment

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

Agree that new probably isn't the best name here, the whole JobHandle infra will be removed once we finish migration to salsa.

let (sender_alive, receiver_alive) = bounded(0);
let (sender_canceled, receiver_canceled) = bounded(0);
let token = JobToken {
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_analysis/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) {
let (offset, code) = extract_offset(text);
let code = code.as_str();

let (_handle, token) = JobHandle::new();
let (_handle, token) = JobHandle::new_pair();
let snap = analysis(&[("/lib.rs", code)]);

snap.resolve_callable(FileId(1), offset, &token).unwrap()
Expand All @@ -71,7 +71,7 @@ fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) {
#[test]
fn test_resolve_module() {
let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]);
let (_handle, token) = JobHandle::new();
let (_handle, token) = JobHandle::new_pair();
let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token);
assert_eq_dbg(
r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_editor/src/extend_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRa
let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start();
let ws_suffix = &ws_text.as_str()[suffix];
let ws_prefix = &ws_text.as_str()[prefix];
if ws_text.contains("\n") && !ws_suffix.contains("\n") {
if ws_text.contains('\n') && !ws_suffix.contains('\n') {
if let Some(node) = ws.next_sibling() {
let start = match ws_prefix.rfind('\n') {
Some(idx) => ws.range().start() + TextUnit::from((idx + 1) as u32),
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_editor/src/folding_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
continue;
}
if node.kind() == COMMENT {
contiguous_range_for_comment(node, &mut visited_comments).map(|range| {
if let Some(range) = contiguous_range_for_comment(node, &mut visited_comments) {
res.push(Fold {
range,
kind: FoldKind::Comment,
})
});
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ra_editor/src/line_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl LineIndex {
let line = self.newlines.upper_bound(&offset) - 1;
let line_start_offset = self.newlines[line];
let col = offset - line_start_offset;
return LineCol {
LineCol {
line: line as u32,
col,
};
}
}

pub fn offset(&self, line_col: LineCol) -> TextUnit {
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_editor/src/scope/fn_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FnScopes {
.syntax()
.descendants()
.filter_map(ast::BindPat::cast)
.filter_map(ScopeEntry::new);
.filter_map(ScopeEntry::new_opt);
self.scopes[scope].entries.extend(entries);
}
fn add_params_bindings(&mut self, scope: ScopeId, params: Option<ast::ParamList>) {
Expand All @@ -88,7 +88,7 @@ pub struct ScopeEntry {
}

impl ScopeEntry {
fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
fn new_opt(pat: ast::BindPat) -> Option<ScopeEntry> {
Copy link
Member

Choose a reason for hiding this comment

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

Let's use new to name such methods? It's what stdlib does, so it looks like a bug in clippy:

https://doc.rust-lang.org/std/ffi/struct.CString.html#method.new

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this will be fixed with rust-lang/rust-clippy#3338

if pat.name().is_some() {
Some(ScopeEntry {
syntax: pat.syntax().owned(),
Expand Down
18 changes: 9 additions & 9 deletions crates/ra_editor/src/scope/mod_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ impl ModuleScope {
let mut entries = Vec::new();
for item in items {
let entry = match item {
ast::ModuleItem::StructDef(item) => Entry::new(item),
ast::ModuleItem::EnumDef(item) => Entry::new(item),
ast::ModuleItem::FnDef(item) => Entry::new(item),
ast::ModuleItem::ConstDef(item) => Entry::new(item),
ast::ModuleItem::StaticDef(item) => Entry::new(item),
ast::ModuleItem::TraitDef(item) => Entry::new(item),
ast::ModuleItem::TypeDef(item) => Entry::new(item),
ast::ModuleItem::Module(item) => Entry::new(item),
ast::ModuleItem::StructDef(item) => Entry::new_item(item),
ast::ModuleItem::EnumDef(item) => Entry::new_item(item),
ast::ModuleItem::FnDef(item) => Entry::new_item(item),
ast::ModuleItem::ConstDef(item) => Entry::new_item(item),
ast::ModuleItem::StaticDef(item) => Entry::new_item(item),
ast::ModuleItem::TraitDef(item) => Entry::new_item(item),
ast::ModuleItem::TypeDef(item) => Entry::new_item(item),
ast::ModuleItem::Module(item) => Entry::new_item(item),
ast::ModuleItem::UseItem(item) => {
if let Some(tree) = item.use_tree() {
collect_imports(tree, &mut entries);
Expand All @@ -50,7 +50,7 @@ impl ModuleScope {
}

impl Entry {
fn new<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> {
fn new_item<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> {
let name = item.name()?;
Some(Entry {
node: name.syntax().owned(),
Expand Down
6 changes: 3 additions & 3 deletions crates/ra_editor/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ pub fn file_structure(file: &File) -> Vec<StructureNode> {
let mut res = Vec::new();
let mut stack = Vec::new();


for event in file.syntax().preorder() {
match event {
WalkEvent::Enter(node) => match structure_node(node) {
Some(mut symbol) => {
WalkEvent::Enter(node) => {
if let Some(mut symbol) = structure_node(node) {
symbol.parent = stack.last().map(|&n| n);
stack.push(res.len());
res.push(symbol);
}
None => (),
},
WalkEvent::Leave(node) => {
if structure_node(node).is_some() {
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_editor/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn join_lines(file: &File, range: TextRange) -> LocalEdit {
pub fn on_enter(file: &File, offset: TextUnit) -> Option<LocalEdit> {
let comment = find_leaf_at_offset(file.syntax(), offset)
.left_biased()
.and_then(|it| ast::Comment::cast(it))?;
.and_then(ast::Comment::cast)?;

if let ast::CommentFlavor::Multiline = comment.flavor() {
return None;
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_lsp_server/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl ConvWith for TextUnit {
fn conv_with(self, line_index: &LineIndex) -> Position {
let line_col = line_index.line_col(self);
// TODO: UTF-16
Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
Position::new(u64::from(line_col.line), u64::from(u32::from(line_col.col)))
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ impl TryConvWith for SourceChange {
.map(|it| it.edits.as_slice())
.unwrap_or(&[]);
let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64);
let position = Position::new(u64::from(line_col.line), u64::from(u32::from(line_col.col)));
Some(TextDocumentPositionParams {
text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
position,
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_lsp_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> Result<()> {
.directory("log")
.start()?;
info!("lifecycle: server started");
match ::std::panic::catch_unwind(|| main_inner()) {
match ::std::panic::catch_unwind(main_inner) {
Ok(res) => {
info!("lifecycle: terminating process with {:?}", res);
res
Expand Down
8 changes: 4 additions & 4 deletions crates/ra_lsp_server/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ pub fn handle_workspace_symbol(
params: req::WorkspaceSymbolParams,
token: JobToken,
) -> Result<Option<Vec<SymbolInformation>>> {
let all_symbols = params.query.contains("#");
let libs = params.query.contains("*");
let all_symbols = params.query.contains('#');
let libs = params.query.contains('*');
let query = {
let query: String = params
.query
Expand Down Expand Up @@ -289,8 +289,8 @@ pub fn handle_runnables(
.filter_map(|ws| {
let tgt = ws.target_by_root(path)?;
Some((
tgt.package(ws).name(ws).clone(),
tgt.name(ws).clone(),
tgt.package(ws).name(ws),
tgt.name(ws),
tgt.kind(ws),
))
})
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_lsp_server/src/main_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl<'a> PoolDispatcher<'a> {
};
match req.cast::<R>() {
Ok((id, params)) => {
let (handle, token) = JobHandle::new();
let (handle, token) = JobHandle::new_pair();
let world = self.world.snapshot();
let sender = self.sender.clone();
self.pool.spawn(move || {
Expand Down
1 change: 0 additions & 1 deletion crates/ra_lsp_server/src/project_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ pub fn workspace_loader() -> (Worker<PathBuf, Result<CargoWorkspace>>, ThreadWat
1,
|input_receiver, output_sender| {
input_receiver
.into_iter()
.map(|path| CargoWorkspace::from_cargo_metadata(path.as_path()))
.for_each(|it| output_sender.send(it))
},
Expand Down
4 changes: 1 addition & 3 deletions crates/ra_lsp_server/src/server_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ impl ServerWorldState {
events
.into_iter()
.map(|event| {
let text = match event.kind {
FileEventKind::Add(text) => text,
};
let FileEventKind::Add(text) = event.kind;
(event.path, text)
})
.map(|(path, text)| (pm.get_or_insert(path, Root::Lib), text))
Expand Down
10 changes: 6 additions & 4 deletions crates/ra_lsp_server/src/thread_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ impl<I, O> Worker<I, O> {
I: Send + 'static,
O: Send + 'static,
{
let ((inp, out), inp_r, out_s) = worker_chan(buf);
let worker = Worker { inp, out };
let (worker, inp_r, out_s) = worker_chan(buf);
let watcher = ThreadWatcher::spawn(name, move || f(inp_r, out_s));
(worker, watcher)
}
Expand Down Expand Up @@ -66,11 +65,14 @@ impl ThreadWatcher {
/// Sets up worker channels in a deadlock-avoind way.
/// If one sets both input and output buffers to a fixed size,
/// a worker might get stuck.
fn worker_chan<I, O>(buf: usize) -> ((Sender<I>, Receiver<O>), Receiver<I>, Sender<O>) {
fn worker_chan<I, O>(buf: usize) -> (Worker<I, O>, Receiver<I>, Sender<O>) {
let (input_sender, input_receiver) = bounded::<I>(buf);
let (output_sender, output_receiver) = unbounded::<O>();
(
(input_sender, output_receiver),
Worker {
inp: input_sender,
out: output_receiver,
},
input_receiver,
output_sender,
)
Expand Down
1 change: 0 additions & 1 deletion crates/ra_lsp_server/src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatc
128,
|input_receiver, output_sender| {
input_receiver
.into_iter()
.map(|path| {
debug!("loading {} ...", path.as_path().display());
let events = load_root(path.as_path());
Expand Down
7 changes: 4 additions & 3 deletions crates/ra_syntax/src/algo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffse
let left = children.next().unwrap();
let right = children.next();
assert!(children.next().is_none());
return if let Some(right) = right {

if let Some(right) = right {
match (
find_leaf_at_offset(left, offset),
find_leaf_at_offset(right, offset),
Expand All @@ -42,10 +43,10 @@ pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffse
}
} else {
find_leaf_at_offset(left, offset)
};
}
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub enum LeafAtOffset<'a> {
None,
Single(SyntaxNodeRef<'a>),
Expand Down
5 changes: 2 additions & 3 deletions crates/ra_syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,8 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> {
type Item = N;
fn next(&mut self) -> Option<N> {
loop {
match N::cast(self.inner.next()?) {
Some(n) => return Some(n),
None => (),
if let Some(n) = N::cast(self.inner.next()?) {
return Some(n);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/ra_syntax/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![
const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW];

pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMarker> {
match literal(p) {
Some(m) => return Some(m),
None => (),
if let Some(m) = literal(p) {
return Some(m);
}
if paths::is_path_start(p) || p.at(L_ANGLE) {
return Some(path_expr(p, r));
Expand Down
6 changes: 2 additions & 4 deletions crates/ra_syntax/src/grammar/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn macro_call(p: &mut Parser) -> BlockLike {
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
p.expect(EXCL);
p.eat(IDENT);
let flavor = match p.current() {
match p.current() {
L_CURLY => {
token_tree(p);
BlockLike::Block
Expand All @@ -365,9 +365,7 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
p.error("expected `{`, `[`, `(`");
BlockLike::NotBlock
}
};

flavor
}
}

pub(crate) fn token_tree(p: &mut Parser) {
Expand Down
Loading