Skip to content

Commit 8c28212

Browse files
authored
Merge pull request #1008 from rust-lang/clippy-channel
Use the currently selected channel to execute Clippy
2 parents bdcd536 + 2d176d6 commit 8c28212

File tree

9 files changed

+81
-71
lines changed

9 files changed

+81
-71
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = {
7474
'reducers/featureFlags.ts',
7575
'reducers/globalConfiguration.ts',
7676
'reducers/output/assembly.ts',
77+
'reducers/output/clippy.ts',
7778
'reducers/output/execute.ts',
7879
'reducers/output/format.ts',
7980
'reducers/output/gist.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ node_modules
2525
!reducers/featureFlags.ts
2626
!reducers/globalConfiguration.ts
2727
!reducers/output/assembly.ts
28+
!reducers/output/clippy.ts
2829
!reducers/output/execute.ts
2930
!reducers/output/format.ts
3031
!reducers/output/gist.ts

ui/frontend/ToolsMenu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as selectors from './selectors';
99
import * as actions from './actions';
1010
import { useAppDispatch } from './configureStore';
1111
import { performFormat } from './reducers/output/format';
12+
import { performClippy } from './reducers/output/clippy';
1213

1314
interface ToolsMenuProps {
1415
close: () => void;
@@ -26,7 +27,7 @@ const ToolsMenu: React.FC<ToolsMenuProps> = props => {
2627

2728
const dispatch = useAppDispatch();
2829
const clippy = useCallback(() => {
29-
dispatch(actions.performClippy());
30+
dispatch(performClippy());
3031
props.close();
3132
}, [dispatch, props]);
3233
const miri = useCallback(() => {

ui/frontend/actions.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ThunkAction as ReduxThunkAction, AnyAction } from '@reduxjs/toolkit';
33

44
import {
55
codeSelector,
6-
clippyRequestSelector,
76
getCrateType,
87
runAsTest,
98
wasmLikelyToWork,
@@ -84,9 +83,6 @@ export enum ActionType {
8483
EnableFeatureGate = 'ENABLE_FEATURE_GATE',
8584
GotoPosition = 'GOTO_POSITION',
8685
SelectText = 'SELECT_TEXT',
87-
RequestClippy = 'REQUEST_CLIPPY',
88-
ClippySucceeded = 'CLIPPY_SUCCEEDED',
89-
ClippyFailed = 'CLIPPY_FAILED',
9086
RequestMiri = 'REQUEST_MIRI',
9187
MiriSucceeded = 'MIRI_SUCCEEDED',
9288
MiriFailed = 'MIRI_FAILED',
@@ -346,42 +342,6 @@ interface GeneralSuccess {
346342
stderr: string;
347343
}
348344

349-
const requestClippy = () =>
350-
createAction(ActionType.RequestClippy);
351-
352-
interface ClippyRequestBody {
353-
code: string;
354-
edition: string;
355-
crateType: string;
356-
}
357-
358-
interface ClippyResponseBody {
359-
success: boolean;
360-
stdout: string;
361-
stderr: string;
362-
}
363-
364-
type ClippySuccess = GeneralSuccess;
365-
366-
const receiveClippySuccess = ({ stdout, stderr }: ClippySuccess) =>
367-
createAction(ActionType.ClippySucceeded, { stdout, stderr });
368-
369-
const receiveClippyFailure = ({ error }: GenericApiFailure) =>
370-
createAction(ActionType.ClippyFailed, { error });
371-
372-
export function performClippy(): ThunkAction {
373-
// TODO: Check a cache
374-
return function(dispatch, getState) {
375-
dispatch(requestClippy());
376-
377-
const body: ClippyRequestBody = clippyRequestSelector(getState());
378-
379-
return jsonPost<ClippyResponseBody>(routes.clippy, body)
380-
.then(json => dispatch(receiveClippySuccess(json)))
381-
.catch(json => dispatch(receiveClippyFailure(json)));
382-
};
383-
}
384-
385345
const requestMiri = () =>
386346
createAction(ActionType.RequestMiri);
387347

@@ -597,9 +557,6 @@ export type Action =
597557
| ReturnType<typeof enableFeatureGate>
598558
| ReturnType<typeof gotoPosition>
599559
| ReturnType<typeof selectText>
600-
| ReturnType<typeof requestClippy>
601-
| ReturnType<typeof receiveClippySuccess>
602-
| ReturnType<typeof receiveClippyFailure>
603560
| ReturnType<typeof requestMiri>
604561
| ReturnType<typeof receiveMiriSuccess>
605562
| ReturnType<typeof receiveMiriFailure>

ui/frontend/reducers/output/clippy.ts

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { Action, ActionType } from '../../actions';
2-
import { finish, start } from './sharedStateManagement';
1+
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
33

4-
const DEFAULT: State = {
4+
import { adaptFetchError, jsonPost, routes } from '../../actions';
5+
import { clippyRequestSelector } from '../../selectors';
6+
import RootState from '../../state';
7+
8+
const sliceName = 'output/clippy';
9+
10+
const initialState: State = {
511
requestsInProgress: 0,
612
};
713

@@ -12,17 +18,48 @@ interface State {
1218
error?: string;
1319
}
1420

15-
export default function clippy(state = DEFAULT, action: Action) {
16-
switch (action.type) {
17-
case ActionType.RequestClippy:
18-
return start(DEFAULT, state);
19-
case ActionType.ClippySucceeded: {
20-
const { stdout = '', stderr = '' } = action;
21-
return finish(state, { stdout, stderr });
22-
}
23-
case ActionType.ClippyFailed:
24-
return finish(state, { error: action.error });
25-
default:
26-
return state;
27-
}
21+
interface ClippyRequestBody {
22+
channel: string;
23+
crateType: string;
24+
edition: string;
25+
code: string;
2826
}
27+
28+
const ClippyResponseBody = z.object({
29+
success: z.boolean(),
30+
stdout: z.string(),
31+
stderr: z.string(),
32+
});
33+
34+
type ClippyResponseBody = z.infer<typeof ClippyResponseBody>;
35+
36+
export const performClippy = createAsyncThunk<ClippyResponseBody, void, { state: RootState }>(
37+
sliceName,
38+
async (_arg: void, { getState }) => {
39+
const body: ClippyRequestBody = clippyRequestSelector(getState());
40+
41+
const d = await adaptFetchError(() => jsonPost(routes.clippy, body));
42+
return ClippyResponseBody.parseAsync(d);
43+
},
44+
);
45+
46+
const slice = createSlice({
47+
name: sliceName,
48+
initialState,
49+
reducers: {},
50+
extraReducers: (builder) => {
51+
builder
52+
.addCase(performClippy.pending, (state) => {
53+
state.requestsInProgress += 1;
54+
})
55+
.addCase(performClippy.fulfilled, (state, action) => {
56+
state.requestsInProgress -= 1;
57+
Object.assign(state, action.payload);
58+
})
59+
.addCase(performClippy.rejected, (state) => {
60+
state.requestsInProgress -= 1;
61+
});
62+
},
63+
});
64+
65+
export default slice.reducer;

ui/frontend/reducers/output/meta.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Draft } from 'immer';
44
import { ActionType } from '../../actions';
55
import { Focus } from '../../types';
66
import { performCompileAssembly } from './assembly';
7+
import { performClippy } from './clippy';
78
import { performExecute, wsExecuteRequest } from './execute';
89
import { performFormat } from './format';
910
import { performGistLoad, performGistSave } from './gist';
@@ -35,7 +36,7 @@ const slice = createSlice({
3536
},
3637
extraReducers: (builder) => {
3738
builder
38-
.addCase(ActionType.RequestClippy, (state) => {
39+
.addCase(performClippy.pending, (state) => {
3940
state.focus = Focus.Clippy;
4041
})
4142

ui/frontend/selectors/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const getStable = (state: State) => state.versions.stable?.rustc;
120120
const getBeta = (state: State) => state.versions.beta?.rustc;
121121
const getNightly = (state: State) => state.versions.nightly?.rustc;
122122
const getRustfmt = createSelector(selectedChannelVersionsSelector, (versions) => versions?.rustfmt);
123-
const getClippy = (state: State) => state.versions.nightly?.clippy;
123+
const getClippy = createSelector(selectedChannelVersionsSelector, (versions) => versions?.clippy);
124124
const getMiri = (state: State) => state.versions?.nightly?.miri;
125125

126126
const versionNumber = (v: Version | undefined) => v ? v.version : '';
@@ -319,10 +319,11 @@ export const anyNotificationsToShowSelector = createSelector(
319319
);
320320

321321
export const clippyRequestSelector = createSelector(
322-
codeSelector,
323-
editionSelector,
322+
channelSelector,
324323
getCrateType,
325-
(code, edition, crateType) => ({ code, edition, crateType }),
324+
editionSelector,
325+
codeSelector,
326+
(channel, crateType, edition, code) => ({ channel, crateType, edition, code }),
326327
);
327328

328329
export const formatRequestSelector = createSelector(

ui/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,13 @@ struct FormatResponse {
374374

375375
#[derive(Debug, Clone, Deserialize)]
376376
struct ClippyRequest {
377-
code: String,
378377
#[serde(default)]
379-
edition: String,
378+
channel: Option<String>,
380379
#[serde(default = "default_crate_type", rename = "crateType")]
381380
crate_type: String,
381+
#[serde(default)]
382+
edition: String,
383+
code: String,
382384
}
383385

384386
#[derive(Debug, Clone, Serialize)]

ui/src/server_axum.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,19 @@ pub(crate) mod api_orchestrator_integration_impls {
11091109

11101110
fn try_from(other: crate::ClippyRequest) -> std::result::Result<Self, Self::Error> {
11111111
let crate::ClippyRequest {
1112-
code,
1113-
edition,
1112+
channel,
11141113
crate_type,
1114+
edition,
1115+
code,
11151116
} = other;
11161117

1118+
let channel = match channel {
1119+
Some(c) => parse_channel(&c)?,
1120+
None => Channel::Nightly,
1121+
};
1122+
11171123
Ok(ClippyRequest {
1118-
channel: Channel::Nightly, // TODO: use what user has submitted
1124+
channel,
11191125
crate_type: parse_crate_type(&crate_type)?,
11201126
edition: parse_edition(&edition)?,
11211127
code,
@@ -1126,10 +1132,13 @@ pub(crate) mod api_orchestrator_integration_impls {
11261132
#[derive(Debug, Snafu)]
11271133
pub(crate) enum ParseClippyRequestError {
11281134
#[snafu(context(false))]
1129-
Edition { source: ParseEditionError },
1135+
Channel { source: ParseChannelError },
11301136

11311137
#[snafu(context(false))]
11321138
CrateType { source: ParseCrateTypeError },
1139+
1140+
#[snafu(context(false))]
1141+
Edition { source: ParseEditionError },
11331142
}
11341143

11351144
impl From<WithOutput<ClippyResponse>> for crate::ClippyResponse {

0 commit comments

Comments
 (0)